875. Koko Eating Bananas

202406301639
tags: #binary-search

var minEatingSpeed = function(piles, h) {
	let min = 1;
	let max = Math.max(...piles);
	let k = max;

	const getHoursNeeded = (bananasPerHour) => piles.reduce((sum, pile) => sum + Math.ceil(pile / bananasPerHour), 0);

	while (min <= max) {
		const mid = Math.floor((max - min) / 2) + min;
		if (getHoursNeeded(mid) <= h) {
			k = mid;
			max = mid - 1;
		} else {
			min = mid + 1;
		}
	}
	return k;
};

Reference

https://leetcode.com/problems/koko-eating-bananas/