Topic link
Leetcode-cn.com/problems/ko…
Their thinking
The main idea of solving the problem is to find the boundary condition of the speed first. The speed can only be 0 ~ the maximum number of bananas on the premise of being able to eat all of them. After finding the boundary condition, the dichotomy search method is used to find the minimum speed as possible
// Pass in all the heaps and the maximum number of hours, then a speed of eating the banana to determine how long it will take to eat all the heaps at this speed. // If greater than the maximum time, then false, if less than or equal, So return true const canEatAll = (piles, H, speed) => {let maxTime = 0; for (let i = 0; i < piles.length; i++) { maxTime += Math.ceil(piles[i] / speed); } return maxTime <= h; } var minEatingSpeed = function (piles can only be eaten once per hour, so if piles are up to the maximum time, Haem.length > h) return false; let max = Math.max(... piles); If (piles === h) piles must be eaten in one hour, then the speed must be the largest one in the piles. // Let min = 0; // Let min = 0; let ret = max; While (min <= Max) {let middle = math.floor ((Max + min) / 2); Const isCanEatAll = canEatAll(piles, H, Middle) if (piles) { If (middle > ret) {Max = middle-1; } else {// If less than, then continue to the left to see if there is a smaller Max = middle-1; ret = middle; }} else {// If (middle + 1) = middle + 1; // If (middle + 1) = middle + 1; } } return ret; };Copy the code