Sliding Window

Find Maximum Window

See my post C++ Maximum Sliding Window Cheatsheet Template!

Template 1: Shrinkable Sliding Window

int i = 0, j = 0, ans = 0;
for (; j < N; ++j) {
    // CODE: use A[j] to update state which might make the window invalid
    for (; invalid(); ++i) { // when invalid, keep shrinking the left edge until it's valid again
        // CODE: update state using A[i]
    }
    ans = max(ans, j - i + 1); // the window [i, j] is the maximum window we've found thus far
}
return ans;

Essentially, we want to keep the window valid at the end of each outer for loop.

Let's apply this template to 1838. Frequency of the Most Frequent Element (Medium).

  1. What should we use as the state? It should be the sum of numbers in the window

  2. How to determine invalid? The window is invalid if (j - i + 1) * A[j] - sum > k. (j - i + 1) is the length of the window [i, j]. We want to increase all the numbers in the window to equal A[j], the number of operations needed is (j - i + 1) * A[j] - sum which should be <= k. For example, assume the window is [1,2,3], increasing all the numbers to 3 will take 3 * 3 - (1 + 2 + 3) operations.

// OJ: https://leetcode.com/problems/frequency-of-the-most-frequent-element/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
    int maxFrequency(vector<int>& A, int k) {
        sort(begin(A), end(A));
        long i = 0, N = A.size(), ans = 1, sum = 0;
        for (int j = 0; j < N; ++j) {
            sum += A[j];
            while ((j - i + 1) * A[j] - sum > k) sum -= A[i++];
            ans = max(ans, j - i + 1);
        }
        return ans;
    }
};

Template 2: Non-shrinkable Sliding Window

Essentially, we GROW the window when it's valid, and SHIFT the window when it's invalid.

Note that there is only a SINGLE for loop now!

Let's apply this template to 1838. Frequency of the Most Frequent Element (Medium) again.

Apply these templates to other problems

Sliding Window (Shrinkable)

  1. What's state? cnt as the number of 0s in the window.

  2. What's invalid? cnt > 1 is invalid.

Sliding Window (Non-shrinkable)

Sliding Window (Shrinkable)

  1. state: cnt[ch] is the number of occurrence of character ch in window.

  2. invalid: cnt[s[j]] > 1 is invalid.

Sliding Window (Non-shrinkable)

Note that since the non-shrinkable window might include multiple duplicates, we need to add a variable to our state.

  1. state: dup is the number of different kinds of characters that has duplicate in the window. For example, if window contains aabbc, then dup = 2 because a and b has duplicates.

  2. invalid: dup > 0 is invalid

Sliding Window (Shrinkable)

  • state: prod is the product of the numbers in window

  • invalid: prod >= k is invalid.

Note that since we want to make sure the window [i, j] is valid at the end of the for loop, we need i <= j check for the inner for loop. i == j + 1 means this window is empty.

Each maximum window [i, j] can generate j - i + 1 valid subarrays, so we need to add j - i + 1 to the answer.

The non-shrinkable template is not applicable here since we need to the length of each maximum window ending at each position

Find Minimum Window

Problems

Find Minimum

Find Maximum

The following problems are also solvable using the shrinkable template with the "At Most to Equal" trick

Find Minimum and Maximum

Fixed-length Sliding Window

Last updated

Was this helpful?