875. Koko Eating Bananas
Solution
卡在如何決定feasible,答案很簡單, 就計算每個pile要花多少時間,然後加總之後看有沒有超過H
public int minEatingSpeed(int[] piles, int H) {
int left = 1;
int right = 0;
for(int pile : piles){
right = Math.max(right, pile);
}
while( right > left ){
int mid = left + (right-left)/2;
if(isfeasible(mid, piles, H)){
right = mid;
}
else{
left = mid+1;
}
}
return left;
}
public boolean isfeasible(int consume, int[] piles, int H){
int day = 0;
for(int pile : piles){
day += pile / consume + ((pile%consume == 0)? 0 : 1);
}
return day <= H;
}
Last updated
Was this helpful?