https://school.programmers.co.kr/learn/courses/30/lessons/340212
코드
function solution(diffs, times, limit) {
let answer = 0;
const check = (level) => {
const requireTime = [times[0]];
for (let i = 1; i < diffs.length; i++) {
const gap = diffs[i] - level;
requireTime.push(times[i]);
if (gap > 0) {
const len = requireTime.length;
requireTime[len - 1] += gap * (times[i] + times[i - 1]);
}
}
const total = requireTime.reduce((acc, cur) => acc + cur, 0);
return total <= limit;
};
let [l, r] = [1, Number.MAX_SAFE_INTEGER];
while (l <= r) {
const mid = Math.floor((l + r) / 2);
if (check(mid)) {
r = mid - 1;
answer = mid;
} else {
l = mid + 1;
}
}
return answer;
}
특정 조건을 만족하는 최솟값을 구하는 문제이기 때문에 파라메트릭 서치(Parametric Search)를 이용하여 풀었다.
'문제풀이 > 이분탐색' 카테고리의 다른 글
[Python/파이썬] 백준 14627번 파닭파닭 (0) | 2025.06.01 |
---|---|
[Python/파이썬] 백준 14233번 악덕 사장 (0) | 2025.04.13 |
[Python/파이썬] 백준 14452번 Cow Dance Show (0) | 2025.04.05 |
[Python/파이썬] LeetCode 4번 Median of Two Sorted Arrays (0) | 2025.03.06 |
[Python/파이썬] 백준 13397번 구간 나누기 2 (0) | 2025.02.18 |