22. Sliding Window & 2 pointer
Conditions for Sliding Window-
- Monotonic condition
- Subarray sum problems with positive numbers
- Window validity behave predictably upon expand / shrink
1. prefix sum + hashmap
2. DP
3. Binary Search
4 Patterns in Sliding Window-
(striver)
❄️ Build 1st window of size k => slide window (L++, R++) => update state =>update ans
-----------------------------------------------------
2. Variable window-1
- (Find longest subarray satisfying a condition )
- for each R, smallest index L, which keeps window valid, is chosen.
❄️expand R => while window invalid: (shrink L) => now, update answer
---------------------------------------------------
3. Variable window-2
- (Count subarrays satisfying a condition )
- (current window invalid => all larger windows are invalid)
- (current window valid => all smaller windows are valid)
- for each R, smallest L which keeps window valid is chosen
❄️expand R => while window invalid: (shrink L) => now, update cnt += (R-L+1)
------------------------------------------------
4. Shortest window⭐
- (find smallest subarray with a given condition )
- (current window invalid => all larger windows are invalid)
- expand till current window is invalid => once valid, now shrink it to make it as small as possible => when it becomes invalid, stop shrinking => continue expanding
❄️ expand R => while window is valid (update ans & shrink L ) => repeat
---------------------------------------------------------------------------------------------------------------------------
(me)
2. At most K bad elements
3. Exactly K elements (via, at most K - at most K-1)
4. Shortest Valid window
--------------------------------------------------------------------------------------------------------------------------
(with all distinct characters)
go through all substrings and check if they contain unique characters or not using a map
1. Try every subarray
2. For every
(i,j): count how many zeros are in v[i..j]
3. If zeros ≤ k, update answer-----------------------------------------------
no. of zeros from i...j = ( pref [j] - pref [i-1] )
---------------------------------------------
3. Fruits into Baskets-
( Longest subarray with at most 2 distinct elements )
Brute- time: O(n^2) space: O(n)
using 2 for loops & a set
Optimal- time: O(2n) space: O(n)
use, sliding window + hashmap
here, k = 2
4. Longest repeating character replacement⭐
Brute-
----------------------------------------------------------------------------------------------------------------------------
5. Count binary subarrays with sum=K
Optimal- time: O(n)
using my sliding window pattern-3 of :
count(=k) = count( <= k) - count( <= k-1)
(i.e., with k odd numbers)
use 2 loops to generate all subarrays and for each subarray from i to j, run another loop to count number of odd numbers in this subarray
-----------
instead of running a third loop in brute force, we would use a prefix array storing number of odd numbers from 0 to index i at pref[i] , thus now we can find number of odd numbers in i to j, as
pref [j] - pref [i-1]
-----------
Optimal- time:O(n)
use sliding window pattern 3 of mine-
count(=k) = count(<=k) - count(<= k-1)
7. Substring containing all 3 characters-⭐
Brute- time: O(n^2) space: O(1)
generate all substrings, keep a track of frequencies of all 3 characters, when frequencies of all three characters in a substring is non zero, then do cnt ++
Better- ⭐
instead of doing cnt++
we will do cnt += (n-j)
since when a first substring containing all 3 characters is found, then all later substrings also counted, hence we do (n-j) to count all later substrings (which are extension of current substring) at once.
----------------------------------------------------------
using minimal valid window concept
for every i, we will find first valid substring starting from i-th index, and then cnt all valid substrings starting from this index...-----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
10. Subarrays with K different integers-
Brute-
--------------------------------------------------Better-
?
-----------------------------------------------------------------------------------------------------------------------------
11. Minimum window substring-
---------------------------------------------------------
Better-
---------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment