코딩 테스트/프로그래머스 level2

징검다리 건너기(이진 탐색 예제)

fullfish 2025. 10. 1. 15:10

def solution(stones, k):
    low, high = 1, max(stones)
    
    def can_cross(n):
        count = 0
        for stone in stones:
            if n >= stone:
                count += 1
                if count == k:
                    return False
            else:
                count = 0
        return True

    while high > low:
        mid = (low + high) // 2
        if can_cross(mid):
            low = mid + 1
        else:
            high = mid
    return low