Skip to content

Commit ddc3a8e

Browse files
authored
Binary Search
1 parent 682abe5 commit ddc3a8e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

56. 875. Koko Eating Bananas

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int minEatingSpeed(int[] piles, int h) {
3+
//find the max ele from piles
4+
int maxEle=Integer.MIN_VALUE;
5+
for(int ele:piles){
6+
maxEle=Math.max(maxEle,ele);
7+
}
8+
9+
int start=1;
10+
int end=maxEle;
11+
//search for the ans
12+
while(start<end){
13+
int mid=start+(end-start)/2;
14+
if(!solve(piles,h,mid)){
15+
start=mid+1;
16+
}else{
17+
end=mid;
18+
}
19+
20+
}
21+
return start;
22+
}
23+
24+
private boolean solve(int []piles,int h,int speed){
25+
int hrs=0;
26+
for(int ele:piles){
27+
hrs += (ele+speed-1)/speed;
28+
}
29+
return hrs<=h;
30+
}
31+
}

0 commit comments

Comments
 (0)