1. Problem

2. Others' Solution
def solution(n):
answer = ''
while n:
if n % 3:
answer += str(n % 3)
n //= 3
else:
answer += "4"
n = n//3 - 1
return answer[::-1]
0,1,2 3개의 숫자를 순서대로 싸이클을 돌리며 수를 표현하는 방법이며 2 이상이 되면 올림수를 만들며 수를 표현함 (10진법은 0~9 까지의 10개의 숫자를 순서대로 싸이클 돌리며 9 이상이되면 올림수를 만들어 10으로 시작)0,1,2 이므로 0,1,2 로 표현한다고 생각할 수 있지만, 다르게 생각한다면 싸이클을 돌릴 수 에서 0번 째, 1번 째, 2번째 수를 나타낸다고 생각도 가능0,1,2 가 아닌 7,8,9 3개의 숫자를 사용하여 표현한다고하면, 7(0) -> 8(1) -> 9(2) -> 87(3) -> 88(4)-> 89(5) 이런식으로 표현할 수도 있음0,1,2가 아닌 1,2,4의 수를 사용하는 것이고, 0이 나타나는 순간은 0 그 자체거나 표현할 수 있는 수의 싸이클을 넘어 갈때 (예들 들면 3진법에서 3 = 10)이므로, 이를 없애기 위해 -1을 수행하고 표현할 수 있는 수를 1씩 증가시키면됨 (3진법에서 10-1 = 2 이지만 2가 3을 표현해야되기 때문에 0,1,2 가 아닌 1,2,3 에 맞추면됨)def solution(n):
answer = ''
while n > 0:
n -= 1
answer = '124'[n%3] + answer
n //= 3
return answer
또는
def solution(n):
if n<=3:
return '124'[n-1]
else:
q, r = divmod(n-1, 3)
return solution(q) + '124'[r]
3. Learned
1. Problem

2. My Solution
# 4가지의 경우
# 1. w = 1 또는 h = 1 인 경우 0개 반환
# 2. w = h 인경우 w개 만큼 빠짐
# 3. w = 홀수 또는 h = 홀수 인 경우 w + h - 1 개 만큼 빠짐 (3x6 규칙 에러)
# 4. w = h = 짝수인 경우 (큰 것을 작은거로 나눈 몫 x 작은 거) 만큼 빠짐
import math
def solution(w,h):
if w == 1 or h == 1:
return 0
if w == h:
return w*h-w
if w % 2 == 1 or h % 2 == 1:
return w*h-(w+h-1)
else:
return w*h - (math.ceil(max(w,h) / min(w,h)) * min(w,h))
3. Others' Solution
1. 대각선을 지나가므로 가로의 길이만큼 가로로 직사각형을 자르고, 세로의 길이만큼 세로로 직사각형을 자름
2. 여기서 중복되는 직사각형을 빼면 됨 w+h-(중복되는 직사각형 수)
3. 중복되는 직사각형의 수 = 최대 공약수
import math
def solution(w,h):
return w*h - (w+h-math.gcd(w,h))
4. Learned
1. Problem

2. My Solution
def solution(progresses, speeds):
answer = []
tasks = []
for task in range(len(progresses)-1,-1,-1):
tasks.append([task,progresses[task]])
for day in range(1,101):
completed_cnt = 0
if not tasks:
break
# 하루가 지나서 진도가 늘어남
for i in range(len(tasks)):
tasks[i][1] += speeds[tasks[i][0]]
while tasks:
if tasks[-1][1] >= 100:
tasks.pop()
completed_cnt += 1
else:
break
if completed_cnt >= 1:
answer.append(completed_cnt)
return answer
3. Learned
1. Problem

2. My Solution
def solution(s):
answer = 0
stack = []
for alphabet in s:
if not stack:
stack.append(alphabet)
elif stack[-1] == alphabet:
stack.pop()
else:
stack.append(alphabet)
return 0 if stack else 1
3. Learned
1. Problem

2. My Solution
def solution(arr):
answer = [arr[0]]
pre_num = arr[0]
for num in arr[1:]:
if num != pre_num:
answer.append(num)
pre_num = num
return answer
3. Others' Solution
answer[-1:] 의 반환값은 리스트이기 때문에 비교할 num 또한 리스트로 변환해야함def solution(arr):
answer = []
for num in arr:
if [num] != answer[-1:]:
answer.append(num)
return answer
4. Learned
from collections import OrderedDict
list(OrderedDict.fromkeys(arr))
1. Problem

2. My Solution
1. 정렬
2. 제일 작은 것이 K 보다 크면 return
3. 가장 작은 것과 2번 쨰로 작은 것을 pop()하여 섞은 다음 push()
4. 1~3 반복
5. 더 이상 섞을 게 없어 1개만 남을 때, 해당 음식의 scoville 이 K 보다 작으면 return -1
1,3 과정에서 시간초과가 발생할 가능성이 높음. deque은 sort 기능이 없기 때문에 heapq 모듈을 사용하여 최소힙(우선순위 큐)로 문제를 해결
3. Learned