본문 바로가기
PROGRAMMERS

[프로그래머스/파이썬] 퍼즐 게임 챌린지

by kode-daks 2025. 2. 4.

내 풀이

def solution(diffs, times, limit):
    answer = 0
    # i-1에서 index 문제가 나지 않도록 0 삽입
    diffs.insert(0, 0)
    times.insert(0, 0)
    
    # limit을 넘는지 확인하는 함수 : 넘지 않으면 True 넘으면 False
    def pnp(level, diffs, times, limit):
        total_time = 0
        for i in range(1, len(diffs)):
            total_time += max(diffs[i]-level, 0) * (times[i] + times[i-1]) + times[i]
        return total_time <= limit
    
    # 이진 탐색
    start, end = 1, max(diffs)
    while start <= end:
        level = int((start+end)/2)
        if pnp(level, diffs, times, limit): # 넘지 않으면 절반의 왼쪽 탐색
            end = level-1
        else: # 넘으면 절반의 오른쪽 탐색
            start = level+1
	
    # 중간점, 중간점의 +- 1 확인
    answer = level if pnp(level, diffs, times, limit) else (level+1 if pnp(level+1, diffs, times, limit) else level-1)
    return answer

 

메모

-