Posts

Showing posts with the label sliding window

1234. Replace the Substring for Balanced String

https://leetcode.com/problems/replace-the-substring-for-balanced-string/ You are given a string containing only 4 kinds of characters  'Q',   'W', 'E'  and  'R' . A string is said to be  balanced   if each of its characters appears  n/4  times where  n  is the length of the string. Return the minimum length of the substring that can be replaced with  any  other string of the same length to make the original string  s   balanced . Return 0 if the string is already  balanced .   Example 1: Input: s = "QWER" Output: 0 Explanation: s is already balanced. Example 2: Input: s = "QQWE" Output: 1 Explanation: We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced. Example 3: Input: s = "QQQW" Output: 2 Explanation: We can replace the first "QQ" to "ER". Example 4: Input: s = "QQQQ" Output: 3 Explanation: We can replace the last 3 'Q' ...

209. Minimum Size Subarray Sum

https://leetcode.com/problems/minimum-size-subarray-sum/ Given an array of  n  positive integers and a positive integer  s , find the minimal length of a  contiguous  subarray of which the sum ≥  s . If there isn't one, return 0 instead. Example:  Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarray [4,3] has the minimal length under the problem constraint. Follow up: If you have figured out the  O ( n ) solution, try coding another solution of which the time complexity is  O ( n  log  n ). --- Time - O(N) Space - O(1) ---

1004. Max Consecutive Ones III

https://leetcode.com/problems/max-consecutive-ones-iii/ Given an array  A  of 0s and 1s, we may change up to  K  values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.  Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0] , K = 2 Output: 6 Explanation: [1,1,1,0,0, 1 ,1,1,1,1, 1 ] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1] , K = 3 Output: 10 Explanation: [0,0, 1,1, 1 , 1 ,1,1,1, 1 ,1,1 ,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i]  is  0  or  1   -----

3. Longest Substring Without Repeating Characters

https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the  longest substring  without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc" , with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: T he answer is "b" , with the length of 1. Example 3: Input: "pwwkew" Output: 3 Explanation: The answer is "wke" , with the length of 3. Note that the answer must be a substring , "pwke" is a subsequence and not a substring. --- Intuition Record unique character, and its last seen position Sliding window - Expand till you see unique characters Shrink when next character is duplicate - shrink window to exclude duplicate character ---