-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.js
More file actions
47 lines (40 loc) · 1.16 KB
/
Copy pathsolution.js
File metadata and controls
47 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @param {number[]} arr
* @param {number} a
* @param {number} b
* @returns {number}
*/
class Solution {
maxSubarrSum(arr, a, b) {
const n = arr.length;
if (n === 0) return 0;
// prefix sums
const prefix = new Array(n + 1).fill(0);
for (let i = 1; i <= n; ++i) prefix[i] = prefix[i - 1] + arr[i - 1];
// deque implemented with array and head pointer for O(1) amortized front pop
const dq = [];
let head = 0;
let ans = Number.NEGATIVE_INFINITY;
for (let r = 1; r <= n; ++r) {
const leftBound = Math.max(0, r - b);
const newIdx = r - a;
// pop front while out of window
while (head < dq.length && dq[head] < leftBound) head++;
// add new index (if valid) maintaining increasing prefix values
if (newIdx >= 0) {
while (
head < dq.length &&
prefix[dq[dq.length - 1]] >= prefix[newIdx]
) {
dq.pop();
}
dq.push(newIdx);
}
if (head < dq.length) {
const candidate = prefix[r] - prefix[dq[head]];
if (candidate > ans) ans = candidate;
}
}
return ans;
}
}