You have n robots. You are given two 0-indexed integer arrays,
chargeTimes and runningCosts, both of length n. The ith robot costs
chargeTimes[i] units to charge and costs runningCosts[i] units to run. You are also given an integer budget.
The total cost of running k chosen robots is equal to `max(chargeTimes)
k * sum(runningCosts), where max(chargeTimes)is the largest charge cost among thekrobots andsum(runningCosts)is the sum of running costs among thek` robots.
Return _themaximum number of consecutive robots you can run such that the total cost does not exceed _budget.
Input: chargeTimes =[3,6,1,3,4], runningCosts =[2,1,3,4,5], budget =25Output: 3Explanation:
It is possible to run all individual and consecutive pairs of robots within budget.To obtain answer 3, consider the first 3 robots. The total cost will be max(3,6,1)+3* sum(2,1,3)=6+3*6=24 which is less than 25.It can be shown that it is not possible to run more than 3 consecutive robots within budget, so we return3.
Input: chargeTimes =[11,12,19], runningCosts =[10,8,7], budget =19Output: 0Explanation: No robot can be run that does not exceed the budget, so we return0.
We want the largest window of consecutive robots such that the cost formula max(chargeTimes) + k * sum(runningCosts) does not exceed the budget. To do this efficiently, we use a sliding window and a monotonic queue to keep track of the maximum charge time in the current window.
classSolution {
public:int maximumRobots(vector<int>& chargeTimes, vector<int>& runningCosts, longlong budget) {
int n = chargeTimes.size(), ans =0;
deque<int> dq;
longlong sum =0;
for (int l =0, r =0; r < n; ++r) {
sum += runningCosts[r];
while (!dq.empty() && chargeTimes[dq.back()] <= chargeTimes[r]) dq.pop_back();
dq.push_back(r);
while (!dq.empty() && chargeTimes[dq.front()] + (r - l +1) * sum > budget) {
if (dq.front() == l) dq.pop_front();
sum -= runningCosts[l++];
}
ans = max(ans, r - l +1);
}
return ans;
}
};
funcmaximumRobots(chargeTimes []int, runningCosts []int, budgetint64) int {
n, ans:= len(chargeTimes), 0dq:= []int{}
sum:= int64(0)
l:=0forr:=0; r < n; r++ {
sum+= int64(runningCosts[r])
for len(dq) > 0&&chargeTimes[dq[len(dq)-1]] <=chargeTimes[r] {
dq = dq[:len(dq)-1]
}
dq = append(dq, r)
for len(dq) > 0&& int64(chargeTimes[dq[0]])+int64(r-l+1)*sum > budget {
ifdq[0] ==l {
dq = dq[1:]
}
sum-= int64(runningCosts[l])
l++ }
ifr-l+1 > ans {
ans = r-l+1 }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintmaximumRobots(int[] chargeTimes, int[] runningCosts, long budget) {
int n = chargeTimes.length, ans = 0;
Deque<Integer> dq =new ArrayDeque<>();
long sum = 0;
for (int l = 0, r = 0; r < n; r++) {
sum += runningCosts[r];
while (!dq.isEmpty() && chargeTimes[dq.peekLast()]<= chargeTimes[r]) dq.pollLast();
dq.addLast(r);
while (!dq.isEmpty() && chargeTimes[dq.peekFirst()]+ (long)(r - l + 1) * sum > budget) {
if (dq.peekFirst() == l) dq.pollFirst();
sum -= runningCosts[l++];
}
ans = Math.max(ans, r - l + 1);
}
return ans;
}
}
classSolution {
funmaximumRobots(chargeTimes: IntArray, runningCosts: IntArray, budget: Long): Int {
val n = chargeTimes.size
var ans = 0val dq = ArrayDeque<Int>()
var sum = 0Lvar l = 0for (r in0 until n) {
sum += runningCosts[r]
while (dq.isNotEmpty() && chargeTimes[dq.last()] <= chargeTimes[r]) dq.removeLast()
dq.addLast(r)
while (dq.isNotEmpty() && chargeTimes[dq.first()] + (r - l + 1) * sum > budget) {
if (dq.first() == l) dq.removeFirst()
sum -= runningCosts[l++]
}
ans = maxOf(ans, r - l + 1)
}
return ans
}
}
classSolution:
defmaximumRobots(self, chargeTimes: list[int], runningCosts: list[int], budget: int) -> int:
from collections import deque
n = len(chargeTimes)
dq = deque()
ans =0 s =0 l =0for r in range(n):
s += runningCosts[r]
while dq and chargeTimes[dq[-1]] <= chargeTimes[r]:
dq.pop()
dq.append(r)
while dq and chargeTimes[dq[0]] + (r - l +1) * s > budget:
if dq[0] == l:
dq.popleft()
s -= runningCosts[l]
l +=1 ans = max(ans, r - l +1)
return ans