其实这个题也可以二分,就是这个二段性全在数目上
C++ 代码
class Solution {
public:
int maximumRobots(vector<int>& chargeTimes, vector<int>& runningCosts, long long budget) {
int n = chargeTimes.size();
int l = 0, r = 0;
int ans = 0;
long long sumT = 0;
deque<int> q;
for (; r < n; r++) {
while (!q.empty() && chargeTimes[q.back()] <= chargeTimes[r]) {
q.pop_back();
}
q.push_back(r);
sumT += runningCosts[r];
while (l <= r && chargeTimes[q.front()] + (r - l + 1) * sumT > budget) {
if (q.front() == l) {
q.pop_front();
}
sumT -= runningCosts[l++];
}
ans = max(ans, r - l + 1);
}
return ans;
}
};