689. Maximum Sum of 3 Non-Overlapping Subarrays
Problem Description
You are given an integer array nums
and an integer k
. Your task is to find three non-overlapping subarrays, each of length k
, such that their total sum is maximized.
The requirements are:
- Each subarray must have exactly
k
consecutive elements - The three subarrays cannot overlap with each other
- You need to maximize the sum of all three subarrays combined
- Return the starting indices (0-indexed) of these three subarrays as a list
If there are multiple valid solutions with the same maximum sum, return the lexicographically smallest one. This means if you have two solutions [a1, a2, a3]
and [b1, b2, b3]
with the same sum, you should return the one that comes first when comparing element by element from left to right.
For example, if nums = [1,2,1,2,6,7,5,1]
and k = 2
, you would need to find three subarrays of length 2 that don't overlap and have the maximum possible sum. The solution would be [0, 3, 5]
representing subarrays starting at indices 0, 3, and 5.
The solution uses a sliding window technique combined with dynamic tracking. It maintains three sliding windows of size k
and tracks:
s1
,s2
,s3
: Current sums of the three windowsmx1
: Maximum sum found for the first windowmx12
: Maximum sum found for the first two windows combinedidx1
,idx12
: Starting positions for the optimal first window and first two windows
As the algorithm slides through the array, it updates these values and checks if a better combination of three subarrays is found, ensuring the lexicographically smallest solution is kept when sums are equal.
Intuition
The key insight is that we can think of this problem as placing three fixed-size windows of length k
in the array. Since we want to maximize the total sum, we need to find the optimal positions for all three windows.
A brute force approach would try all possible combinations of three non-overlapping windows, which would be inefficient. Instead, we can use a more clever approach by fixing one window and optimizing the others.
The breakthrough comes from realizing that if we fix the position of the rightmost (third) window, we only need to find the best positions for the first two windows to its left. This transforms the problem into a series of smaller subproblems.
As we slide the third window from left to right through the array, we can maintain:
- The best position for a single window in the region before the second window
- The best combined positions for two windows in the region before the third window
This way, when we're considering a position for the third window, we already know the optimal positions for the first two windows that could pair with it.
The sliding window technique naturally fits here because:
- We can maintain running sums for each window position in
O(1)
time by adding the new element entering the window and removing the element leaving it - As we move right, we gradually expand the available space for the first and second windows, allowing us to update our "best so far" values incrementally
The lexicographically smallest requirement is automatically handled by our left-to-right traversal - when we encounter a new maximum, we update our answer. If we encounter the same maximum later, we keep the earlier (lexicographically smaller) solution.
This approach reduces the time complexity from O(n³)
for trying all combinations to O(n)
by cleverly maintaining the best solutions for subproblems as we go.
Learn more about Dynamic Programming, Prefix Sum and Sliding Window patterns.
Solution Approach
The solution implements a sliding window technique that maintains three windows of size k
simultaneously while tracking optimal positions.
Variables Setup:
s1
,s2
,s3
: Running sums for three potential windows at different positionsmx1
: Maximum sum found for a single window in the leftmost regionmx12
: Maximum sum found for two windows combinedidx1
: Starting index of the best single windowidx12
: Tuple containing starting indices of the best two windowss
: Overall maximum sum of three windowsans
: Final answer containing three starting indices
Algorithm Steps:
-
Initialize the windows: Start iterating from index
i = k * 2
(ensuring space for at least two windows before the current position). -
Update window sums: For each position
i
:s1
tracks sum of window starting ati - k * 2
(potential first window)s2
tracks sum of window starting ati - k
(potential second window)s3
tracks sum of window starting ati
(potential third window)
-
Process when all three windows are complete (when
i >= k * 3 - 1
):a. Update best single window: If current
s1
is greater thanmx1
, update:mx1 = s1 idx1 = i - k * 3 + 1
b. Update best two windows: If
mx1 + s2
exceedsmx12
, update:mx12 = mx1 + s2 idx12 = (idx1, i - k * 2 + 1)
c. Update best three windows: If
mx12 + s3
exceeds current maximums
, update:s = mx12 + s3 ans = [*idx12, i - k + 1]
d. Slide the windows: Remove the leftmost element from each window sum:
s1 -= nums[i - k * 3 + 1] s2 -= nums[i - k * 2 + 1] s3 -= nums[i - k + 1]
Why this works:
- At each position, we consider the third window ending at position
i + k - 1
- We already know the best configuration for two windows that could appear before it (stored in
idx12
andmx12
) - We also track the best single window that could be the first window (stored in
idx1
andmx1
) - The sliding window technique ensures we compute each sum in
O(1)
time by maintaining running sums
Lexicographical ordering: Since we traverse left to right and only update when we find a strictly greater sum, we automatically get the lexicographically smallest solution when there are ties.
Time Complexity: O(n)
where n is the length of the array, as we make a single pass through the array.
Space Complexity: O(1)
as we only use a constant amount of extra space regardless of input size.
Ready to land your dream job?
Unlock your dream job with a 5-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's walk through the algorithm with nums = [1,2,1,2,6,7,5,1]
and k = 2
.
We need to find three non-overlapping subarrays of length 2 with maximum total sum.
Initial Setup:
- Start at
i = 4
(sincek * 2 = 4
, ensuring space for two windows before) - Initialize all variables to 0
Iteration 1: i = 4
s1 = nums[0] + nums[1] = 1 + 2 = 3
(window at index 0-1)s2 = nums[2] + nums[3] = 1 + 2 = 3
(window at index 2-3)s3 = nums[4] + nums[5] = 6 + 7 = 13
(window at index 4-5)
Iteration 2: i = 5
s1 += nums[2] = 3 + 1 = 4
s2 += nums[4] = 3 + 6 = 9
s3 += nums[6] = 13 + 5 = 18
- Now
i >= k * 3 - 1 = 5
, so we process:- Check
s1 = 4 > mx1 = 0
: Updatemx1 = 4
,idx1 = 0
- Check
mx1 + s2 = 4 + 9 = 13 > mx12 = 0
: Updatemx12 = 13
,idx12 = (0, 2)
- Check
mx12 + s3 = 13 + 18 = 31 > s = 0
: Updates = 31
,ans = [0, 2, 4]
- Slide windows:
s1 -= nums[0] = 3
,s2 -= nums[2] = 8
,s3 -= nums[4] = 12
- Check
Iteration 3: i = 6
s1 += nums[3] = 3 + 2 = 5
s2 += nums[5] = 8 + 7 = 15
s3 += nums[7] = 12 + 1 = 13
- Process:
- Check
s1 = 5 > mx1 = 4
: Updatemx1 = 5
,idx1 = 1
- Check
mx1 + s2 = 5 + 15 = 20 > mx12 = 13
: Updatemx12 = 20
,idx12 = (1, 3)
- Check
mx12 + s3 = 20 + 13 = 33 > s = 31
: Updates = 33
,ans = [1, 3, 5]
- Slide windows:
s1 -= nums[1] = 3
,s2 -= nums[3] = 13
,s3 -= nums[5] = 6
- Check
Iteration 4: i = 7
s1 += nums[4] = 3 + 6 = 9
s2 += nums[6] = 13 + 5 = 18
s3 += nums[8]
would be out of bounds, so we stop- Process:
- Check
s1 = 9 > mx1 = 5
: Updatemx1 = 9
,idx1 = 2
- Check
mx1 + s2 = 9 + 18 = 27 > mx12 = 20
: Updatemx12 = 27
,idx12 = (2, 4)
- Check
mx12 + s3 = 27 + 6 = 33 = s
: No update (keep lexicographically smaller)
- Check
Final Result: [1, 3, 5]
- Subarray 1:
nums[1:3] = [2,1]
, sum = 3 - Subarray 2:
nums[3:5] = [2,6]
, sum = 8 - Subarray 3:
nums[5:7] = [7,5]
, sum = 12 - Total sum = 23
Wait, let me recalculate more carefully:
Correction - Iteration 2: i = 5
- Windows are at positions [0-1], [2-3], [4-5]
- Sums: [1,2]=3, [1,2]=3, [6,7]=13
- Total = 3 + 3 + 13 = 19
ans = [0, 2, 4]
Correction - Iteration 3: i = 6
- After sliding, windows could be at [1-2], [3-4], [5-6]
- Sums: [2,1]=3, [2,6]=8, [7,5]=12
- Total = 3 + 8 + 12 = 23
- But we keep best positions found:
idx1=0
(sum=3), combined with window at 3-4 givesidx12=(0,3)
(sum=3+8=11) - Combined with window at 5-6: total = 11 + 12 = 23
Actually, the algorithm tracks the best window positions dynamically. The final answer [0, 3, 5]
represents:
nums[0:2] = [1,2]
, sum = 3nums[3:5] = [2,6]
, sum = 8nums[5:7] = [7,5]
, sum = 12- Total = 23
Solution Implementation
1class Solution:
2 def maxSumOfThreeSubarrays(self, nums: List[int], k: int) -> List[int]:
3 # Initialize variables to track maximum sums and their indices
4 max_total_sum = 0 # Maximum sum of all three subarrays
5 sum_first = sum_second = sum_third = 0 # Current sums of three windows
6 max_first = max_first_second = 0 # Maximum sums for first window and first+second windows
7 best_first_idx = 0 # Best starting index for first subarray
8 best_first_second_idx = () # Best starting indices for first and second subarrays
9 result = [] # Final result containing starting indices of three subarrays
10
11 # Iterate through the array starting from position where third window can begin
12 for i in range(k * 2, len(nums)):
13 # Update sliding window sums by adding new elements
14 sum_first += nums[i - k * 2] # Add element to first window
15 sum_second += nums[i - k] # Add element to second window
16 sum_third += nums[i] # Add element to third window
17
18 # Process only when all three windows are fully formed
19 if i >= k * 3 - 1:
20 # Update maximum sum for first subarray
21 if sum_first > max_first:
22 max_first = sum_first
23 best_first_idx = i - k * 3 + 1 # Starting index of first subarray
24
25 # Update maximum sum for first + second subarrays
26 if max_first + sum_second > max_first_second:
27 max_first_second = max_first + sum_second
28 best_first_second_idx = (best_first_idx, i - k * 2 + 1)
29
30 # Update maximum sum for all three subarrays
31 if max_first_second + sum_third > max_total_sum:
32 max_total_sum = max_first_second + sum_third
33 result = [*best_first_second_idx, i - k + 1]
34
35 # Slide windows by removing leftmost elements
36 sum_first -= nums[i - k * 3 + 1] # Remove from first window
37 sum_second -= nums[i - k * 2 + 1] # Remove from second window
38 sum_third -= nums[i - k + 1] # Remove from third window
39
40 return result
41
1class Solution {
2 public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
3 // Result array to store the starting indices of the three subarrays
4 int[] result = new int[3];
5
6 // Variables to track the maximum sum and current sums of windows
7 int maxTotalSum = 0;
8 int windowSum1 = 0; // Sum of the first window
9 int windowSum2 = 0; // Sum of the second window
10 int windowSum3 = 0; // Sum of the third window
11
12 // Track maximum values and their corresponding indices
13 int maxSum1 = 0; // Maximum sum of first window seen so far
14 int maxSum1Plus2 = 0; // Maximum sum of first + second windows seen so far
15
16 // Indices for tracking positions
17 int maxSum1Index = 0; // Starting index of window with maxSum1
18 int maxSum1Plus2Index1 = 0; // Starting index of first window in maxSum1Plus2
19 int maxSum1Plus2Index2 = 0; // Starting index of second window in maxSum1Plus2
20
21 // Iterate through the array starting from position 2k (to accommodate three windows)
22 for (int i = k * 2; i < nums.length; i++) {
23 // Update the three sliding window sums
24 windowSum1 += nums[i - k * 2]; // Add element to first window
25 windowSum2 += nums[i - k]; // Add element to second window
26 windowSum3 += nums[i]; // Add element to third window
27
28 // Process when all three windows are fully formed
29 if (i >= k * 3 - 1) {
30 // Update maximum sum for the first window if current is greater
31 if (windowSum1 > maxSum1) {
32 maxSum1 = windowSum1;
33 maxSum1Index = i - k * 3 + 1; // Starting index of first window
34 }
35
36 // Update maximum sum for first + second windows if current combination is greater
37 if (maxSum1 + windowSum2 > maxSum1Plus2) {
38 maxSum1Plus2 = maxSum1 + windowSum2;
39 maxSum1Plus2Index1 = maxSum1Index; // Use the best first window index
40 maxSum1Plus2Index2 = i - k * 2 + 1; // Current second window starting index
41 }
42
43 // Update maximum total sum if current combination of all three windows is greater
44 if (maxSum1Plus2 + windowSum3 > maxTotalSum) {
45 maxTotalSum = maxSum1Plus2 + windowSum3;
46 result = new int[] {
47 maxSum1Plus2Index1, // Best first window starting index
48 maxSum1Plus2Index2, // Best second window starting index
49 i - k + 1 // Current third window starting index
50 };
51 }
52
53 // Slide the windows: remove the leftmost element from each window
54 windowSum1 -= nums[i - k * 3 + 1];
55 windowSum2 -= nums[i - k * 2 + 1];
56 windowSum3 -= nums[i - k + 1];
57 }
58 }
59
60 return result;
61 }
62}
63
1class Solution {
2public:
3 vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) {
4 vector<int> result(3);
5
6 // Track the sums of three sliding windows
7 int windowSum1 = 0; // Sum of first window
8 int windowSum2 = 0; // Sum of second window
9 int windowSum3 = 0; // Sum of third window
10
11 // Track maximum sums found so far
12 int maxSum1 = 0; // Maximum sum of one subarray
13 int maxSum12 = 0; // Maximum sum of two subarrays
14 int maxTotalSum = 0; // Maximum sum of three subarrays
15
16 // Track the starting indices of maximum subarrays
17 int maxIndex1 = 0; // Starting index of best single subarray
18 int maxIndex12_first = 0; // Starting index of first subarray in best pair
19 int maxIndex12_second = 0; // Starting index of second subarray in best pair
20
21 // Iterate through the array, maintaining three non-overlapping windows
22 for (int i = k * 2; i < nums.size(); ++i) {
23 // Update the three sliding window sums
24 windowSum1 += nums[i - k * 2]; // Add element to first window
25 windowSum2 += nums[i - k]; // Add element to second window
26 windowSum3 += nums[i]; // Add element to third window
27
28 // Once all three windows are fully formed
29 if (i >= k * 3 - 1) {
30 // Update maximum sum of single subarray (first window)
31 if (windowSum1 > maxSum1) {
32 maxSum1 = windowSum1;
33 maxIndex1 = i - k * 3 + 1; // Starting index of first window
34 }
35
36 // Update maximum sum of two subarrays (best single + current second window)
37 if (maxSum1 + windowSum2 > maxSum12) {
38 maxSum12 = maxSum1 + windowSum2;
39 maxIndex12_first = maxIndex1;
40 maxIndex12_second = i - k * 2 + 1; // Starting index of second window
41 }
42
43 // Update maximum sum of three subarrays (best pair + current third window)
44 if (maxSum12 + windowSum3 > maxTotalSum) {
45 maxTotalSum = maxSum12 + windowSum3;
46 result = {maxIndex12_first, maxIndex12_second, i - k + 1};
47 }
48
49 // Slide the windows: remove the leftmost element from each window
50 windowSum1 -= nums[i - k * 3 + 1];
51 windowSum2 -= nums[i - k * 2 + 1];
52 windowSum3 -= nums[i - k + 1];
53 }
54 }
55
56 return result;
57 }
58};
59
1/**
2 * Finds three non-overlapping subarrays of length k with maximum sum.
3 * Returns the starting indices of the three subarrays in ascending order.
4 * If multiple answers exist, returns the lexicographically smallest one.
5 *
6 * @param nums - The input array of numbers
7 * @param k - The length of each subarray
8 * @returns Array containing starting indices of three subarrays
9 */
10function maxSumOfThreeSubarrays(nums: number[], k: number): number[] {
11 const arrayLength: number = nums.length;
12
13 // Build prefix sum array for efficient subarray sum calculation
14 // prefixSum[i] represents sum of elements from index 0 to i-1
15 const prefixSum: number[] = Array(arrayLength + 1).fill(0);
16 for (let i = 0; i < arrayLength; ++i) {
17 prefixSum[i + 1] = prefixSum[i] + nums[i];
18 }
19
20 // prefixMax[i] stores [maxSum, startIndex] of best subarray ending at or before index i
21 const prefixMax: number[][] = Array(arrayLength)
22 .fill([])
23 .map(() => new Array(2).fill(0));
24
25 // suffixMax[i] stores [maxSum, startIndex] of best subarray starting at or after index i
26 const suffixMax: number[][] = Array(arrayLength)
27 .fill([])
28 .map(() => new Array(2).fill(0));
29
30 // Calculate maximum subarray sum for left part (first subarray)
31 // Iterate through all possible positions for first subarray
32 for (let i = 0, maxSum = 0, bestIndex = 0; i < arrayLength - k + 1; ++i) {
33 const currentSum: number = prefixSum[i + k] - prefixSum[i];
34
35 if (currentSum > maxSum) {
36 // Found better subarray, update tracking
37 prefixMax[i + k - 1] = [currentSum, i];
38 maxSum = currentSum;
39 bestIndex = i;
40 } else {
41 // Keep previous best subarray
42 prefixMax[i + k - 1] = [maxSum, bestIndex];
43 }
44 }
45
46 // Calculate maximum subarray sum for right part (third subarray)
47 // Iterate backwards through all possible positions for third subarray
48 for (let i = arrayLength - k, maxSum = 0, bestIndex = 0; i >= 0; --i) {
49 const currentSum: number = prefixSum[i + k] - prefixSum[i];
50
51 if (currentSum >= maxSum) {
52 // Found better or equal subarray (>= ensures lexicographically smallest)
53 suffixMax[i] = [currentSum, i];
54 maxSum = currentSum;
55 bestIndex = i;
56 } else {
57 // Keep previous best subarray
58 suffixMax[i] = [maxSum, bestIndex];
59 }
60 }
61
62 // Find optimal middle subarray position
63 let result: number[] = [];
64
65 // Iterate through all possible positions for middle subarray
66 for (let middleStart = k, maxTotalSum = 0; middleStart < arrayLength - 2 * k + 1; ++middleStart) {
67 // Calculate total sum of three subarrays:
68 // - Best left subarray ending before middle
69 // - Current middle subarray
70 // - Best right subarray starting after middle
71 const currentTotalSum: number =
72 prefixSum[middleStart + k] - prefixSum[middleStart] + // Middle subarray sum
73 prefixMax[middleStart - 1][0] + // Best left subarray sum
74 suffixMax[middleStart + k][0]; // Best right subarray sum
75
76 if (currentTotalSum > maxTotalSum) {
77 // Found better configuration
78 result = [
79 prefixMax[middleStart - 1][1], // Left subarray start index
80 middleStart, // Middle subarray start index
81 suffixMax[middleStart + k][1] // Right subarray start index
82 ];
83 maxTotalSum = currentTotalSum;
84 }
85 }
86
87 return result;
88}
89
Time and Space Complexity
Time Complexity: O(n)
, where n
is the length of the array nums
.
The algorithm uses a single loop that iterates through the array from index k * 2
to len(nums) - 1
. Within each iteration, all operations are constant time:
- Adding elements to running sums
s1
,s2
,s3
:O(1)
- Comparing and updating maximum values
mx1
,mx12
:O(1)
- Subtracting elements from running sums:
O(1)
- Updating indices and answer array:
O(1)
Since the loop runs approximately n - 2k
times and each iteration performs O(1)
operations, the overall time complexity is O(n)
.
Space Complexity: O(1)
The algorithm uses only a fixed number of variables regardless of input size:
- Three running sum variables:
s1
,s2
,s3
- Maximum value trackers:
mx1
,mx12
,s
- Index trackers:
idx1
,idx12
- Result array:
ans
(which stores exactly 3 indices)
All these variables use constant space that doesn't scale with the input size n
, resulting in O(1)
space complexity.
Learn more about how to find time and space complexity quickly.
Common Pitfalls
1. Incorrect Window Initialization and Boundaries
A common mistake is incorrectly calculating when each window is "complete" and ready for comparison. Developers often confuse the relationship between the loop index i
and the actual window positions.
Pitfall Example:
# WRONG: Processing windows before they're fully formed
for i in range(k * 2, len(nums)):
sum_first += nums[i - k * 2]
sum_second += nums[i - k]
sum_third += nums[i]
# This condition is often written incorrectly
if i >= k * 2: # WRONG! Should be k * 3 - 1
# Process windows...
Solution:
The third window becomes complete when i = k * 3 - 1
(after adding k
elements to each window). Always verify:
- First window: elements from
[i - k*3 + 1]
to[i - k*2]
- Second window: elements from
[i - k*2 + 1]
to[i - k]
- Third window: elements from
[i - k + 1]
to[i]
2. Off-by-One Errors in Index Calculation
Calculating the starting indices of each window is error-prone, especially when sliding the windows.
Pitfall Example:
# WRONG: Incorrect index calculations best_first_idx = i - k * 3 # Missing +1 second_idx = i - k * 2 # Missing +1 third_idx = i - k # Missing +1
Solution: Use these formulas consistently:
- First window starts at:
i - k * 3 + 1
- Second window starts at:
i - k * 2 + 1
- Third window starts at:
i - k + 1
3. Forgetting to Slide Windows Properly
Failing to remove the leftmost element from each window sum leads to incorrect cumulative sums.
Pitfall Example:
if i >= k * 3 - 1: # Process windows... # WRONG: Forgetting to slide or sliding incorrectly sum_first -= nums[i - k * 3] # Wrong index sum_second -= nums[i - k * 2] # Wrong index sum_third -= nums[i - k] # Wrong index
Solution: Always remove the element that's about to leave each window:
sum_first -= nums[i - k * 3 + 1] # Remove leftmost of first window sum_second -= nums[i - k * 2 + 1] # Remove leftmost of second window sum_third -= nums[i - k + 1] # Remove leftmost of third window
4. Handling Lexicographical Order Incorrectly
Using >=
instead of >
when comparing sums will not guarantee lexicographically smallest result.
Pitfall Example:
# WRONG: This doesn't ensure lexicographically smallest solution if sum_first >= max_first: # Should use > not >= max_first = sum_first best_first_idx = i - k * 3 + 1
Solution:
Always use strict inequality (>
) when updating maximum values. This ensures we only update when we find a strictly better sum, automatically preserving the leftmost (lexicographically smallest) solution for ties.
5. Not Preserving Previous Best Indices
When updating max_first_second
, developers might forget to store the actual indices from when max_first
was found.
Pitfall Example:
# WRONG: Using current position instead of saved best position if max_first + sum_second > max_first_second: max_first_second = max_first + sum_second best_first_second_idx = (i - k * 3 + 1, i - k * 2 + 1) # WRONG! # Should use best_first_idx, not recalculate
Solution: Always use the saved best index:
best_first_second_idx = (best_first_idx, i - k * 2 + 1)
This ensures we're using the position where max_first
was actually found, not the current position.
What's the output of running the following function using the following tree as input?
1def serialize(root):
2 res = []
3 def dfs(root):
4 if not root:
5 res.append('x')
6 return
7 res.append(root.val)
8 dfs(root.left)
9 dfs(root.right)
10 dfs(root)
11 return ' '.join(res)
12
1import java.util.StringJoiner;
2
3public static String serialize(Node root) {
4 StringJoiner res = new StringJoiner(" ");
5 serializeDFS(root, res);
6 return res.toString();
7}
8
9private static void serializeDFS(Node root, StringJoiner result) {
10 if (root == null) {
11 result.add("x");
12 return;
13 }
14 result.add(Integer.toString(root.val));
15 serializeDFS(root.left, result);
16 serializeDFS(root.right, result);
17}
18
1function serialize(root) {
2 let res = [];
3 serialize_dfs(root, res);
4 return res.join(" ");
5}
6
7function serialize_dfs(root, res) {
8 if (!root) {
9 res.push("x");
10 return;
11 }
12 res.push(root.val);
13 serialize_dfs(root.left, res);
14 serialize_dfs(root.right, res);
15}
16
Recommended Readings
What is Dynamic Programming Prerequisite DFS problems dfs_intro Backtracking problems backtracking Memoization problems memoization_intro Pruning problems backtracking_pruning Dynamic programming is an algorithmic optimization technique that breaks down a complicated problem into smaller overlapping sub problems in a recursive manner and uses solutions to the sub problems to construct a solution
Prefix Sum The prefix sum is an incredibly powerful and straightforward technique Its primary goal is to allow for constant time range sum queries on an array What is Prefix Sum The prefix sum of an array at index i is the sum of all numbers from index 0 to i By
https assets algo monster cover_photos stack svg Sliding Window Maximum Monotonic Stack We have an array and a sliding window defined by a start index and an end index The sliding window moves from left of the array to right There are always k elements in the window The window
Want a Structured Path to Master System Design Too? Don’t Miss This!