二分答案题目
思路
使用二分找到最小满足条件的解
细节
设被除数 $m$,除数 $k$,则两者相除上取整有三种情况
① $m$ 能被 $k$ 整除
② $m < k$
③ $m > k$
代码实现
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int n = piles.size();
int l = 1, r = 1e9 + 10;
while (l < r) {
int mid = l + r >> 1;
int sum = 0;
for (int i = 0; i < n; i ++ ) {
if (piles[i] % mid == 0) sum += piles[i] / mid;
else sum += piles[i] / mid + 1;
}
if (sum <= h) r = mid;
else l = mid + 1;
}
return l;
}
};