Skip to content

Commit 1870c52

Browse files
committed
feat: problems of next week
1 parent 657e5fc commit 1870c52

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} chargeTimes
3+
* @param {number[]} runningCosts
4+
* @param {number} budget
5+
* @return {number}
6+
*/
7+
const maximumRobots = function (c, r, b) {
8+
const n = c.length; const q = []
9+
let ans = 0
10+
for (let i = 0, j = 0, sum = 0; i < n; i++) {
11+
// [i, j)
12+
while (j < n) {
13+
if (j >= i) {
14+
// 如果加上 j 会出问题,就停止
15+
const max = Math.max(c[j], q.length ? c[q[0]] : 0)
16+
if (max + (j - i + 1) * (sum + r[j]) > b) break
17+
18+
while (q.length && c[q[q.length - 1]] <= c[j]) q.pop()
19+
q.push(j)
20+
}
21+
sum += r[j]
22+
23+
j++
24+
}
25+
// console.log(i, j, sum, q[0], q.length ? c[q[0]] : 0)
26+
ans = Math.max(ans, j - i)
27+
28+
while (q.length && q[0] <= i) q.shift()
29+
sum -= r[i]
30+
}
31+
return ans
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const shortestSubarray = function (nums, k) {
7+
const n = nums.length
8+
const sum = [0]; const q = [0]; let ans = 1e10
9+
for (let i = 1; i <= n; i++) {
10+
sum[i] = sum[i - 1] + nums[i - 1]
11+
// 尽量短
12+
while (q.length && sum[i] - sum[q[1]] >= k) q.shift()
13+
if (sum[i] - sum[q[0]] >= k) ans = Math.min(i - q[0], ans)
14+
while (q.length && sum[q[q.length - 1]] >= sum[i]) q.pop()
15+
q.push(i)
16+
}
17+
return ans === 1e10 ? -1 : ans
18+
}

0 commit comments

Comments
 (0)