Skip to content

Commit 472c6a5

Browse files
committed
Time: 37 ms (30.37%) | Memory: 21.7 MB (48.93%) - LeetSync
1 parent b05a782 commit 472c6a5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int maxele(vector<int>& piles, int n){
4+
int maxi = INT_MIN;
5+
for(int i = 0; i < n; i++){
6+
maxi = max(maxi, piles[i]);
7+
}
8+
return maxi;
9+
}
10+
long long func(vector<int>& piles, int k, int n){
11+
long long totalH = 0;
12+
for(int i = 0; i < n; i++){
13+
totalH += ceil((double)(piles[i])/ (double)(k));
14+
}
15+
return totalH;
16+
}
17+
int minEatingSpeed(vector<int>& piles, int h) {
18+
int n = piles.size();
19+
int low = 1;
20+
int high = maxele(piles, n);
21+
int ans = 1;
22+
while(low <= high){
23+
int mid = low + (high - low) / 2;
24+
long long totalH = func(piles, mid, n);
25+
if(totalH <= h){
26+
ans = mid;
27+
high = mid - 1;
28+
}
29+
else low = mid + 1;
30+
}
31+
return ans;
32+
}
33+
};

0 commit comments

Comments
 (0)