二分法找适当最小值问题

二分法找适当最小值问题

 

 有一点对数组的用法与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;
    }
}

 

上一篇:循环移位(Cycle)


下一篇:P1288 取数游戏II