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가지 경우가 무엇인지 아직 생각아 안난다.
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에 기인하지 않고 대진을 리셋하면서 올라간다 결과적으로 이 문제는 문제는 장황하지만 이진 탐색을 하는 문제이다

+ Recent posts