-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path875.java
More file actions
68 lines (61 loc) · 1.68 KB
/
875.java
File metadata and controls
68 lines (61 loc) · 1.68 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
__________________________________________________________________________________________________
sample 2 ms submission
class Solution
{
public int minEatingSpeed(int[] piles, int H)
{
int max = 0;
long sum = 0;
for (int pile : piles)
{
max = Math.max(max, pile);
sum += pile;
}
if (piles.length == H)
{
return max;
}
int lowerbound = (int) (sum / (long) H) + 1;
while (true)
{
int hours = 0;
for (int pile : piles)
{
hours += (pile - 1) / lowerbound + 1;
}
if (hours <= H)
{
return lowerbound;
}
++lowerbound;
}
}
}
__________________________________________________________________________________________________
sample 40500 kb submission
class Solution {
public int minEatingSpeed(int[] piles, int H) {
int lo = 1;
int hi = 0;
for(int num : piles) {
hi = Math.max(hi, num);
}
while(lo < hi) {
int mid = lo + (hi - lo) / 2;
if(canEatAll(piles, mid, H)) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
private boolean canEatAll(int[] piles, int K, int H) {
int hours = 0;
for(int num : piles) {
hours += num % K == 0? num / K : num / K + 1;
}
return hours <= H;
}
}
__________________________________________________________________________________________________