class Solution {
public int minEatingSpeed(int[] piles, int h) {
int min = 1;//Atleast one banana coco can eat
int max = Integer.MIN_VALUE;//max would be the max value in the piles[] array
for(int i =0;i< piles.length;i++){
max = Math.max(max, piles[i]);
}
int result = Integer.MAX_VALUE;
while(min<=max){
int mid = (min+max)/2;
if(possible(mid,piles,h)){//if this is possible then there could be smaller value of k that is also possible
result = Math.min(result, mid);
max = mid-1;
}
else min = mid+1;
}
return result;
}
public boolean possible (int k, int piles[] , int h){
long time =0;
for(int p : piles){
time+=p/k;
if(p%k!=0) time++;
}
return time<=h;
}
}
Similar Questions:
Top comments (0)