Skip to content

Commit 1c888cb

Browse files
committed
Time: 72 ms (17.03%), Space: 18.7 MB (80.45%) - LeetHub
1 parent c67b12f commit 1c888cb

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
4+
bool isPoss(vector<int>& piles, int h, int speed, int n) {
5+
int cnt = 0;
6+
for(int i = 0; i < n; i++) {
7+
// 3 15
8+
if(piles[i] <= speed){
9+
cnt++;
10+
}else {
11+
// 17 5
12+
cnt += piles[i] / speed;
13+
if(piles[i] % speed != 0)
14+
cnt++;
15+
}
16+
}
17+
18+
return cnt <= h;
19+
}
20+
21+
int minEatingSpeed(vector<int>& piles, int h) {
22+
// k - [3, ,x ,11]
23+
int n = piles.size();
24+
sort(piles.begin(), piles.end());
25+
int low = 1;
26+
int high = piles[n - 1];
27+
28+
int ans = high;
29+
while(low <= high) {
30+
int mid = (low + high) / 2;
31+
if(isPoss(piles, h, mid, n)) {
32+
ans = mid;
33+
high = mid - 1;
34+
}else
35+
low = mid + 1;
36+
}
37+
38+
return ans;
39+
}
40+
};

0 commit comments

Comments
 (0)