반응형
# 내가 푼 방식 (시간이 좀 걸림...)
def solution(n,a,b):
li = [i+1 for i in range(n)]
cnt, flag = 0, False
while True:
cnt += 1
new_li = []
for i in range(len(li)//2):
if li[i*2] in [a,b] and li[i*2+1] in [a,b]:
flag = True
break
elif li[i*2] in [a,b]: new_li.append(li[i*2])
else : new_li.append(li[i*2+1])
li = new_li
if flag: break
return cnt
# 좋게 느껴진 코드
def solution(n,a,b):
answer = 0
while a != b:
answer += 1
a, b = (a+1)//2, (b+1)//2
return answer
'알고리즘' 카테고리의 다른 글
[프로그래머스] N개의 최소공배수 (0) | 2023.06.13 |
---|---|
[프로그래머스] 점프와 순간 이동 (0) | 2023.06.12 |
[프로그래머스] 개인정보 수집 유효기간 (0) | 2023.06.09 |
[프로그래머스] 카펫 (0) | 2023.06.06 |
[프로그래머스] 영어 끝말잇기 (0) | 2023.06.05 |