Skip to content

Commit 299963e

Browse files
authored
Update 2398-maximum-number-of-robots-within-budget.js
1 parent d0b1f9d commit 299963e

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

2398-maximum-number-of-robots-within-budget.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
* @param {number} budget
55
* @return {number}
66
*/
7-
var maximumRobots = function(chargeTimes, runningCosts, budget) {
8-
let times = chargeTimes
9-
let costs = runningCosts
10-
let sum = 0;
11-
let i = 0, n = times.length;
12-
const d = [];
13-
for (let j = 0; j < n; ++j) {
14-
sum += costs[j];
15-
while (d.length && times[d[d.length - 1]] <= times[j]) d.pop();
16-
d.push(j);
17-
if (times[d[0]] + (j - i + 1) * sum > budget) {
18-
if (d[0] == i) d.shift();
19-
sum -= costs[i++];
20-
}
7+
const maximumRobots = function(chargeTimes, runningCosts, budget) {
8+
const times = chargeTimes, costs = runningCosts
9+
let sum = 0, res = 0, j = 0
10+
const q = [], n = times.length
11+
for(let i = 0; i < n; i++) {
12+
sum += costs[i]
13+
while(q.length && times[q[q.length - 1]] <= times[i]) q.pop()
14+
q.push(i)
15+
16+
if(q.length && times[q[0]] + (i - j + 1) * sum > budget) {
17+
if(q[0] === j) q.shift()
18+
sum -= costs[j]
19+
j++
20+
}
21+
res = Math.max(res, i - j + 1)
2122
}
22-
return n - i;
23+
24+
return res
2325
};
2426

0 commit comments

Comments
 (0)