跳至主要內容

875. 爱吃香蕉的珂珂

T4mako算法数组二分查找小于 1 分钟

875. 爱吃香蕉的珂珂

中等

题目描述open in new window

解题思路:
定义 left = 1,right = Integer.MAX_VALUE
使用二分查找,计算时间 sum

  • sum <= hres = Math.min(mid,res);right = mid - 1;
  • sum > hleft = mid + 1;
class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        int res = Integer.MAX_VALUE;
        int left = 1;
        int right = Integer.MAX_VALUE;
        while (left <= right){
            long sum = 0;
            int mid = left + (right - left) / 2;
            for (int pile : piles) {
                sum += pile % mid == 0 ? pile / mid : pile / mid + 1;
            }
            if(sum <= h){res = Math.min(mid,res);right = mid - 1;}
            else {left = mid + 1;}
        }
        return res;
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5