본문 바로가기

컴퓨터/코테

프로그래머스 징검다리 건너기

def solution(stones, k):
    minimum = 0
    maximum = 200000000
    person = maximum // 2
    while True:
    	# 건널 수 있나?
        possible = True
        cnt = 0
        for i in stones : 
            if i < person : 
                cnt += 1
            else : 
                cnt = 0
            if cnt == k : 
                possible = False
                break

		# 건널 수 있으면
        if possible :
            minimum = person
            next = (person + maximum) // 2
            if person == next :
                return person
            person = next
        # 건널 수 없으면
        else :
            maximum = person
            next = (person + minimum) // 2
            if person == next :
                return person - 1
            person = next

이진 탐색 오랜만에 하는거라 재밌었다 ㅋㅋㅋㅋ 곧 있으면 프로그래머스 1400점 갈듯