-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
36 lines (29 loc) · 1.15 KB
/
Copy pathsolution.java
File metadata and controls
36 lines (29 loc) · 1.15 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
import java.util.ArrayDeque;
import java.util.Deque;
class Solution {
// Use long to avoid overflow of sums
public long maxSubarrSum(int[] arr, int a, int b) {
int n = arr.length;
if (n == 0) return 0L;
long[] prefix = new long[n + 1];
prefix[0] = 0L;
for (int i = 1; i <= n; ++i) prefix[i] = prefix[i - 1] + (long)arr[i - 1];
Deque<Integer> dq = new ArrayDeque<>();
long ans = Long.MIN_VALUE;
for (int r = 1; r <= n; ++r) {
int leftBound = Math.max(0, r - b);
int newIdx = r - a;
// remove indices that are out of the window
while (!dq.isEmpty() && dq.peekFirst() < leftBound) dq.pollFirst();
// add new index (if available) maintaining increasing prefix values
if (newIdx >= 0) {
while (!dq.isEmpty() && prefix[dq.peekLast()] >= prefix[newIdx]) dq.pollLast();
dq.offerLast(newIdx);
}
if (!dq.isEmpty()) {
ans = Math.max(ans, prefix[r] - prefix[dq.peekFirst()]);
}
}
return ans;
}
}