当前位置:网站首页>2022.6.7-----leetcode. eight hundred and seventy-five

2022.6.7-----leetcode. eight hundred and seventy-five

2022-06-10 04:45:00 Lu 727

 public int minEatingSpeed(int[] piles, int h) {
        int n=piles.length;
        if(n==1) return piles[0]%h==0?piles[0]/h:piles[0]/h+1;
        int l=1,r=0;
        // Find the maximum speed 
        for(int i=0;i<n;i++){
            if(piles[i]>r) r=piles[i];  
        }
        
        while(l<r){
            int mid=l+r>>1;
            int sum=0;// Total use time 
            for(int i=0;i<n;i++){
                 sum+=piles[i]/mid;
                if(piles[i]%mid!=0) sum++;
            }
            if(sum>h) l=mid+1;
            else r=mid;// Not more than , The current speed may be the minimum speed , Need to keep 
        }
        return l;
    }

原网站

版权声明
本文为[Lu 727]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091228491243.html