Sliding Window Technique Problem
Problem A sliding-window problem: given an array and a window definition, compute an aggregate over contiguous subarrays — e.g. the maximum sum of a subarray of fixed size k, or the smallest window satisfying a given constraint.
Input / Output
- Input: array
numsof integers, plus a window sizek(fixed-size variant) or a target condition (variable-size variant) - Output: the required aggregate — max/min sum, count of qualifying windows, or the length of the smallest/longest valid window
Constraints
- 1 <= nums.length <= 10^5, so an O(n*k) brute force will time out
- 1 <= k <= nums.length
- Values may be negative in the fixed-size variant; the variable-size shrink trick requires non-negative values
Example
- Input:
nums = [2,1,5,1,3,2],k = 3→ Output:9(window[5,1,3]) - Tricky case:
nums = [-1,-2,-3],k = 2→-3; the answer must be seeded from the first window, not from 0
asked …