forked from USPCodeLabSanca/dev.hire-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path875.cpp
More file actions
38 lines (30 loc) · 901 Bytes
/
875.cpp
File metadata and controls
38 lines (30 loc) · 901 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
38
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int start = 1;
int end = piles[0];
int currentMid = 1;
for (int i = 0; i < piles.size(); i++) {
if (piles[i] > end)
end = piles[i];
}
while (start <= end) {
int mid = start + (end - start) / 2;
int time = this->timeToEat(piles, mid);
if (time > h) {
start = mid + 1;
} else {
end = mid - 1;
currentMid = mid;
}
}
return currentMid;
}
int timeToEat(vector<int>& piles, int k) {
int sum = 0;
for (int i = 0; i < piles.size(); i++) {
sum += (int) ceil(((double) piles[i]) / ((double) k));
}
return sum;
}
};