-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path875.cpp
More file actions
33 lines (28 loc) · 724 Bytes
/
875.cpp
File metadata and controls
33 lines (28 loc) · 724 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
class Solution {
public:
bool canEat(vector<int>& piles, int speed, int h) {
long int hours = 0;
for (int p: piles) {
hours += p / speed;
if (p % speed != 0) {
hours ++;
}
}
return hours <= h;
}
int minEatingSpeed(vector<int>& piles, int h) {
int left = 1;
long int right = *max_element(piles.begin(),piles.end());
int ans;
while (left <= right) {
int mid = (left+right) / 2;
if (canEat(piles, mid, h)) {
ans = mid;
right = mid-1;
} else {
left = mid+1;
}
}
return ans;
}
};