其他分享
首页 > 其他分享> > 【875. 爱吃香蕉的珂珂】二分查找

【875. 爱吃香蕉的珂珂】二分查找

作者:互联网

class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        int ans = 0;
        int low = 1;
        int high = 1000000001;

        while(low<high){
            int mid = ( low +high)/2;
            if(check(piles,mid,h)){
                high = mid;
            }else{
                //太小;
                low = mid+1;
            }
        }
        return low;
    }

    /**
     *
     * 如果滿足,返回true,否則返回false
     *
     * @param piles
     * @param x
     * @param h
     * @return
     */
    public boolean check(int[] piles,int x,int h){
        int ans = 0;
        for(int pile:piles){
            ans += pile/x;
            if(pile%x != 0){
                ans+=1;
            }
        }
        return ans<=h;
    }
}

 

标签:二分,piles,int,minEatingSpeed,875,Solution,查找,low
来源: https://www.cnblogs.com/fishcanfly/p/16352955.html