LeetCode 1011. 在 D 天内送达包裹的能力
原题链接
中等
作者:
回归线
,
2021-04-26 14:14:53
,
所有人可见
,
阅读 231
class Solution {
private:
bool check(vector<int>& weights, int D, int threshold) {
int days = 1;
int sum = 0;
for (int w: weights) {
if (w > threshold) {
return false;
}
sum += w;
if (sum > threshold) {
sum = w;
++days;
}
}
return days <= D;
}
public:
int shipWithinDays(vector<int>& weights, int D) {
int l = 0;
int r = 500 * 50000;
while (l < r) {
const int mid = l + r >> 1;
if (check(weights, D, mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return r;
}
};