-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
31 lines (26 loc) · 863 Bytes
/
Copy pathmain.cpp
File metadata and controls
31 lines (26 loc) · 863 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
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int maxPile = piles[0];
for (int i = 1; i < piles.size(); i++) {
if (maxPile < piles[i]) {
maxPile = piles[i];
}
}
int left = 1;
int right = maxPile;
while (left <= right) {
int mid = left + (right - left) / 2;
long long hourNeeded = 0;
for (int i = 0; i < piles.size(); i++) {
hourNeeded += (piles[i] + mid - 1) / mid;
}
if (hourNeeded <= h) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
};