Opening the reading…
Opening the reading…
SLIDING WINDOW › FIXED SIZE SLIDING-WINDOW
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →Every valid answer consists of three windows in order: a left window, a middle window, and a right window. Once the middle window starts at mid, the left window must finish before mid, and the right window must start after mid + k. The values inside those two regions no longer interact, so the best answer for this middle window is the best compatible left window plus the best compatible right window.
First compute the sum of every length-k window. Then, for every possible boundary, store which window is best among all windows ending before that boundary and which window is best among all windows starting after it. The final scan chooses each possible middle window once and combines it with those two precomputed choices. Equal sums need deliberate tie rules because the required answer is lexicographically smallest, not merely optimal.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | The window sums, left array, right array, and middle scan each process O(n) or O(m) entries. Each stage advances monotonically and performs only constant work per entry, so no element is revisited inside a nested scan. |
| Space | O(n) extra | The window sums and two best-index arrays each contain m entries, so their combined working storage is O(m) = O(n). The returned three-index array is required output and is excluded; the worst shape changes the constant count of stored arrays, not the asymptotic bound. |
#include <climits>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) {
int n = static_cast<int>(nums.size());
int m = n - k + 1;
vector<long long> window(m);
long long current = 0;
for (int i = 0; i < k; ++i) {
current += nums[i];
}
window[0] = current;
for (int start = 1; start < m; ++start) {
current += nums[start + k - 1];
current -= nums[start - 1];
window[start] = current;
}
vector<int> left(m);
int best = 0;
for (int i = 0; i < m; ++i) {
if (window[i] > window[best]) {
best = i;
}
left[i] = best;
}
vector<int> right(m);
best = m - 1;
for (int i = m - 1; i >= 0; --i) {
if (window[i] >= window[best]) {
best = i;
}
right[i] = best;
}
long long bestTotal = LLONG_MIN;
vector<int> answer(3);
for (int mid = k; mid < m - k; ++mid) {
int l = left[mid - k];
int r = right[mid + k];
long long total = window[l] + window[mid] + window[r];
vector<int> candidate = {l, mid, r};
if (total > bestTotal ||
(total == bestTotal && candidate < answer)) {
bestTotal = total;
answer = candidate;
}
}
return answer;
}
};The strict comparison in the left pass preserves the first index for an equal sum. The greater-than-or-equal comparison in the right-to-left pass replaces an equal choice with the earlier index. The final vector comparison is still useful: it makes the complete lexicographic rule visible and protects the result even when several middle positions produce the same total.