1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import math; def solution(n, a, b): min_ = min (a, b); max_ = max (a, b); root_temp = n / / 2 ; temp = root_temp; while True : if min_ > temp : # 두개 모두 temp 보다 큼 root_temp / / = 2 ; temp + = root_temp; continue ; elif max_ < temp: root_temp / / = 2 ; temp - = root_temp; continue ; elif min_ = = temp: print ( "min" ) return int (math.log(root_temp, 2 )) + 1 elif max_ = = temp: return int (math.log(root_temp, 2 )) else : return int (math.log(root_temp, 2 )) + 1 ; |
위는 91점 코드인데 안되는 3가지 경우가 무엇인지 아직 생각아 안난다.
1 2 3 4 5 6 7 8 | import math def solution(n,a,b): a, b = min (a,b), max (a,b) for k in range ( 1 , int (math.log2(n) + 1 )): if a % 2 ! = 0 and a + 1 = = b: return k else : n, a, b = n / 2 , math.ceil(a / 2 ), math.ceil(b / 2 ) ### |
이게 다른 방법, temp에 기인하지 않고 대진을 리셋하면서 올라간다 결과적으로 이 문제는 문제는 장황하지만 이진 탐색을 하는 문제이다
'1일 1문제, 프로그래머스' 카테고리의 다른 글
2017 팁스타운) 짝지어 제거하기, 60점.. (0) | 2018.08.26 |
---|---|
이제는 1주에 1문제 내지 2문제로... ㅠ_ㅠ (0) | 2018.08.21 |
레벨2, N개의 최소 공배수 (0) | 2018.08.12 |
프로그래머스, 레벨3, 야근지수(못품) (0) | 2018.08.02 |
프로그래머스, 레벨3, 가장 긴 펠린드롬 (0) | 2018.07.31 |