-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
37 lines (35 loc) · 804 Bytes
/
Copy pathSolution.java
File metadata and controls
37 lines (35 loc) · 804 Bytes
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
class Solution
{
public int minEatingSpeed(int[] piles, int H)
{
int max = 0;
for (int pile : piles)
{
max = Math.max(max, pile);
}
int l = 1, r = max;
while (l < r)
{
int mid = (r + l) / 2;
int numberOfHoursNeeded = _calculateHoursNeeded(piles, mid);
if (numberOfHoursNeeded > H)
{
l = mid + 1;
}
else
{
r = mid;
}
}
return r;
}
private int _calculateHoursNeeded(int[] piles, int mid)
{
int hoursNeeded = 0;
for (int pile : piles)
{
hoursNeeded += pile / mid + (pile % mid != 0 ? 1 : 0);
}
return hoursNeeded;
}
}