Skip to content

Commit d0b1f9d

Browse files
authored
Create 2398-maximum-number-of-robots-within-budget.js
1 parent 9eea8a2 commit d0b1f9d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} chargeTimes
3+
* @param {number[]} runningCosts
4+
* @param {number} budget
5+
* @return {number}
6+
*/
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+
}
21+
}
22+
return n - i;
23+
};
24+

0 commit comments

Comments
 (0)