Skip to main content

🟡 剑指 Offer II 073. 狒狒吃香蕉

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解1#

想复杂了。直接二分法找最小速度即可。

class Solution {    public int minEatingSpeed(int[] piles, int h) {        int left = 1, right = 1_000_000_000, mid;
        while (left < right) {            mid = (left + right) / 2;            if (isPossible(piles, mid, h)) {                right = mid;            } else {                left = mid+1;            }        }
        return left;    }
    private boolean isPossible(int[] piles, int speed, int h) {        int times = 0;        for (int pile : piles) {            times += (int)Math.ceil((double)pile/speed);        }        return times <= h;    }}