-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkokoeating.java
More file actions
38 lines (37 loc) · 1.16 KB
/
kokoeating.java
File metadata and controls
38 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class kokoeating
{
public int minEatingSpeed(int[] piles, int h)
{
int low = 1, high = findMax(piles);
//apply binary search:
while (low <= high) {
int mid = (low + high) / 2;
int totalH = calculateTotalHours(piles, mid);
if (totalH <= h) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return low;
}
public static int calculateTotalHours(int[] piles, int h) {
int totalH = 0;
int n = piles.length;
//find total hours:
for (int i = 0; i < n; i++) {
totalH += Math.ceil((double)(piles[i]) / (double)(h));
}
return totalH;
}
public static int findMax(int[] piles) {
int maxi = Integer.MIN_VALUE;;
int n = piles.length;
//find the maximum:
for (int i = 0; i < n; i++) {
maxi = Math.max(maxi, piles[i]);
}
return maxi;
}
}
}