diff --git "a/\342\234\205 Pattern 01 : Sliding Window.md" "b/\342\234\205 Pattern 01 : Sliding Window.md" index fa3083a..100a54b 100644 --- "a/\342\234\205 Pattern 01 : Sliding Window.md" +++ "b/\342\234\205 Pattern 01 : Sliding Window.md" @@ -1,7 +1,7 @@ # Pattern 1: Sliding Window In many problems dealing with an array (or a LinkedList), we are asked to find or calculate something among all the contiguous subarrays (or sublists) of a given size. For example, take a look at this problem: -### Find Averages of Sub Arrays +### Find Averages of Sub Arrays ✅ https://leetcode.com/problems/maximum-average-subarray-i/ > Given an array, find the average of all contiguous subarrays of size `K` in it. @@ -206,7 +206,8 @@ smallestSubarrayWithGivenSum([3, 4, 1, 1, 6], 8)//3 - The time complexity of the above algorithm will be `O(N)`. The outer for loop runs for all elements, and the inner while loop processes each element only once; therefore, the time complexity of the algorithm will be `O(N+N)`), which is asymptotically equivalent to `O(N)`. - The algorithm runs in constant space `O(1)`. -## Longest Substring with K Distinct Characters (medium) +## Longest Substring with K Distinct Characters (medium) ✅ [solution](https://www.geeksforgeeks.org/problems/longest-k-unique-characters-substring0853/1) + https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ >Given a string, find the length of the longest substring in it with no more than `K` distinct characters. @@ -265,7 +266,7 @@ longestSubstringWithKdistinct("cbbebi", 3)//5, The longest substrings with no mo - The above algorithms time complexity will be `O(N)`, where `N` is the number of characters in the input string. The outer for loop runs for all characters, and the inner while loop processes each character only once; therefore, the time complexity of the algorithm will be `O(N+N)`, which is asymptotically equivalent to `O(N)` - The algorithms space complexity is `O(K)`, as we will be storing a maximum of `K+1` characters in the HashMap. -## 🔎 Fruits into Baskets (medium) +## 🔎 Fruits into Baskets (medium) ✅ https://leetcode.com/problems/fruit-into-baskets/ > Given an array of characters where each character represents a fruit tree, you are given two baskets, and your goal is to put the maximum number of fruits in each basket. The only restriction is that each basket can have only one type of fruit. @@ -359,7 +360,7 @@ fruitsInBaskets(['A', 'B', 'C', 'B', 'B', 'C'])//5 , We can put 3 'B' in one bas ```` - The above algorithms time complexity will be `O(N)`, where `N` is the number of characters in the input array. The outer `for` loop runs for all characters, and the inner `while` loop processes each character only once; therefore, the time complexity of the algorithm will be `O(N+N)`, which is asymptotically equivalent to `O(N)`. - The algorithm runs in constant space `O(1)` as there can be a maximum of three types of fruits stored in the frequency map. -### Longest Substring with at most 2 distinct characters +### Longest Substring with at most 2 distinct characters ✅ https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ > Given a string, find the length of the longest substring in it with at most two distinct characters. @@ -402,7 +403,7 @@ lengthOfLongestSubstringTwoDistinct('ccaabbb')//5 -## No-repeat Substring (hard) +## No-repeat Substring (hard) ✅ https://leetcode.com/problems/longest-substring-without-repeating-characters/ > Given a string, find the length of the longest substring, which has no repeating characters. @@ -450,7 +451,7 @@ nonRepeatSubstring("abccde")//3 - The above algorithms time complexity will be `O(N)`, where `N` is the number of characters in the input string. - The algorithms space complexity will be `O(K)`, where `K` is the number of distinct characters in the input string. This also means `K<=N`, because in the worst case, the whole string might not have any repeating character, so the entire string will be added to the HashMap. Having said that, since we can expect a fixed set of characters in the input string (e.g., 26 for English letters), we can say that the algorithm runs in fixed space `O(1)`; in this case, we can use a fixed-size array instead of the HashMap. -## Longest Substring with Same Letters after Replacement (hard) +## Longest Substring with Same Letters after Replacement (hard) ✅ https://leetcode.com/problems/longest-repeating-character-replacement/ > Given a string with lowercase letters only, if you are allowed to replace no more than `K` letters with any letter, find the length of the longest substring having the same letters after replacement. @@ -504,7 +505,7 @@ lengthOfLongestSubstring("abccde", 1)//3, Replace the 'b' or 'd' with 'c' to hav - The above algorithms time complexity will be `O(N)`, where `N` is the number of letters in the input string. - As we expect only the lower case letters in the input string, we can conclude that the space complexity will be `O(26)` to store each letters frequency in the HashMap, which is asymptotically equal to `O(1)`. -## Longest Subarray with Ones after Replacement (hard) +## Longest Subarray with Ones after Replacement (hard) ✅ https://leetcode.com/problems/max-consecutive-ones-iii/ > Given an array containing `0`'s and `1`'s, if you are allowed to replace no more than `K` `0`'s with `1`'s,