내 풀이
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
메모
-
'PROGRAMMERS' 카테고리의 다른 글
[프로그래머스/파이썬] 게임 맵 최단거리 (1) | 2025.02.06 |
---|---|
[프로그래머스/파이썬] 석유 시추 (0) | 2025.02.06 |
[프로그래머스/파이썬] 주차 요금 계산 (0) | 2025.02.03 |
[프로그래머스/파이썬] 택배상자 (0) | 2025.01.31 |
[프로그래머스/파이썬] 스킬트리 (0) | 2025.01.24 |