Skip to content
Algorithm.js
📚 단일 패턴

875. Koko Eating Bananas

이진 탐색으로 코코가 주어진 h 시간 안에 모든 바나나를 먹을 수 있는 최소 속도를 찾는 방법 정리

Oct 4, 2025 — binary-search

문제 설명

풀이 아이디어

해결 전략

const isOK = (speed) => {
let times = 0;
for (const pile of piles) {
times += Math.ceil(pile/speed);
}
return times <= h;
}

구현

/**
* @param {number[]} piles
* @param {number} h
* @return {number}
*/
var minEatingSpeed = function(piles, h) {
const isOK = (speed) => {
let times = 0;
for (const pile of piles) {
times += Math.ceil(pile/speed);
}
return times <= h;
}
let left = 1;
let right = 10 ** 9;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (isOK(mid)) right = mid - 1;
else left = mid + 1;
}
return left;
};