Review: Slidingwindow Problems Posted on July 29, 2018July 26, 2020 by braindenny Slidingwindow Problems Basic Abstractions Name Summary Move right pointer l+k-1 (l: left, k: window length) Move left pointer r-k+1 (r: right, k: window length) Collect results r-k+1>=0 Make sure neither pointers will go backwards Scenarios Top Questions Num Problem Summary 1 Sliding window with fixed size LeetCode: Find All Anagrams in a String 2 Sliding window with non-decreasing size LeetCode: Max Consecutive Ones III 3 How to initialize the time window? LeetCode: Minimum Swaps to Group All 1’s Together 4 Sliding window with non-decreasing size LeetCode: Max Consecutive Ones III 5 Move two pointers: two loop vs One loop LeetCode: Longest Substring Without Repeating Characters 6 Inspiring sliding window problem LeetCode: Moving Stones Until Consecutive II 7 Sliding window with lazy removal Leetcode: Sliding Window Median 8 Sliding window with adjustable size 9 Move pointer1 to match the other, or the other way around Code Skeleton Sliding window with fixed size // Blog link: https://code.dennyzhang.com/minimum-swaps-to-group-all-1s-together // Basic Ideas: fixed size sliding window // We know the size of the final group all 1's // The window can start from any position // Use a sliding window. And the find the minimum count of 0s in the sliding window. // Complexity: Time O(n), Space O(k) func minSwaps(data []int) int { k:=0 for _, v := range data { if v == 1 { k++ } } res := 1<<31-1 count := 0 // sliding window. left: i-k+1, right: i for i:=0; i<len(data); i++ { // move right pointer: add a new element if data[i] == 0 { count++ } // move left pointer: remove the old element if i-k >= 0 { if data[i-k] == 0 { count-- } } // collect result if i-k+1 >= 0 { if count < res { res = count } } } return res } CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups See all slidingwindow problems: #slidingwindow Review: Slidingwindow ProblemsLeetCode: Video StitchingLeetCode: Twitch WordsLeetCode: Swap For Longest Repeated Character SubstringLeetCode: Subarrays with K Different IntegersLeetCode: Sliding Window MedianLeetCode: Sliding Window MaximumLeetCode: Shortest Way to Form StringLeetCode: Permutation in StringLeetCode: Number of Substrings Containing All Three CharactersLeetCode: Number of Sub-arrays of Size K and Average Greater than or Equal to ThresholdLeetCode: Moving Stones Until Consecutive IILeetCode: Minimum Window SubstringLeetCode: Minimum Swaps to Group All 1’s TogetherLeetCode: Minimum Number of Taps to Open to Water a GardenLeetCode: Maximum Number of Vowels in a Substring of Given LengthLeetCode: Maximum Average Subarray IILeetCode: Maximum Average Subarray ILeetCode: Max Consecutive Ones IIILeetCode: Max Chunks To Make SortedLeetCode: Longest Turbulent SubarrayLeetCode: Longest Substring Without Repeating CharactersLeetCode: Longest Substring with At Most Two Distinct CharactersLeetCode: Longest Substring with At Most K Distinct CharactersLeetCode: Longest Repeating Character ReplacementLeetCode: Grumpy Bookstore OwnerLeetCode: Get Equal Substrings Within BudgetLeetCode: Find K-th Smallest Pair DistanceLeetCode: Find All Anagrams in a StringLeetCode: Diet Plan PerformanceLeetCode: Count Substrings with Only One Distinct LetterLeetCode: Contains Duplicate IILeetCode: Constrained Subset SumLeetCode: Check If a String Contains All Binary Codes of Size K See more blog posts. Post Views: 11 Post navigation LeetCode: Stone GameLeetCode: Middle of the Linked List Leave a Reply Cancel replyYour email address will not be published.Comment Name Email Website