其他分享
首页 > 其他分享> > 二分法找适当最小值问题

二分法找适当最小值问题

作者:互联网

 

 有一点对数组的用法与Math方法取整用法记录一下吧

class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        int left =0;
        int right = Arrays.stream(piles).max().getAsInt();
        while(left < right){
            int mid = (left+right)/2;
            if(check(piles,mid,h)){
                right = mid;
            }else{
                left = mid+1;
            }
        }
        return left;
    }
    public boolean check(int[] piles,int spead,int h){
        int hours = 0;
        for(int p:piles){
            hours += Math.ceil((double)p/spead);
        }
        return hours <= h;
    }
}

 

标签:hours,right,piles,int,mid,二分法,最小值,适当,left
来源: https://www.cnblogs.com/Dkfeng/p/15227189.html