标签:char ini cab cond bigger red tput ble push
For most substring problem, we are given a string and need to find a substring of it which satisfy some restrictions.
A general way is to use a hashmap assisted with two pointers. The template is given below. int findSubstring(string s){ vector<int> map(128,0); int counter; // check whether the substring is valid int begin=0, end=0; //two pointers, one point to tail and one head int d; //the length of substring for() { /* initialize the hash map here */ } while(end<s.size()){ if(map[s[end++]]-- ?){ /* modify counter here */ } while(/* counter condition */){ /* update d here if finding minimum*/ //increase begin to make it invalid/valid again if(map[s[begin++]]++ ?){ /*modify counter here*/ } } /* update d here if finding maximum*/ } return d; } One thing needs to be mentioned is that when asked to find maximum substring,
we should update maximum after the inner while loop to guarantee that the substring is valid.
On the other hand, when asked to find minimum substring, we should update minimum inside the inner while loop.
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.
1 class Solution { 2 public: 3 int minSubArrayLen(int s, vector<int>& nums) { 4 const int n = nums.size(); 5 int l = 0, r = 0, sum = s, res = n + 1; 6 while (r < n) { 7 sum -= nums[l]; 8 while (r + 1 < n && sum > 0) { 9 sum -= nums[++r]; 10 } 11 res = min(res, r - l); 12 l = ++r; 13 } 14 return res; 15 } 16 };
Variation 2:
1 class Solution { 2 public: 3 int minSubArrayLen(int s, vector<int>& nums) { 4 if (nums.size() == 0) return 0; 5 int size = nums.size(); 6 int l = 0; 7 int sum = 0; 8 int ret = INT_MAX; 9 for (int i = 0; i < size; ++i) { 10 sum += nums[i]; 11 while (sum >= s) { 12 ret = min(ret, i - l + 1); 13 sum -= nums[l++]; 14 } 15 } 16 return ret == INT_MAX ? 0 : ret; 17 } 18 };
Variation 3:
1 class Solution { 2 public: 3 int minSubArrayLen(int s, vector<int>& nums) { 4 if (nums.size() == 0) return 0; 5 int size = nums.size(); 6 int l = 0; 7 int sum = 0; 8 int ret = INT_MAX; 9 for (int i = 0; i < size; ++i) { 10 sum += nums[i]; 11 while (sum >= s) { 12 ret = min(ret, i - l + 1); 13 sum -= nums[l++]; 14 } 15 } 16 return ret == INT_MAX ? 0 : ret; 17 } 18 };
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/
Return the length of the shortest, non-empty, contiguous subarray of A
with sum at least K
.
If there is no non-empty subarray with sum at least K
, return -1
.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
Everything is the same, except with negative integer(s) in the array
Q: Why keep the deque increase?
A: If B[i] <= B[d.back()] and moreover we already know that i > d.back(), it means that compared with d.back(),
B[i] can help us make the subarray length shorter and sum bigger. So no need to keep d.back() in our deque.
More detailed on this, we always add at the LAST position
B[d.back] <- B[i] <- ... <- B[future id]
B[future id] - B[d.back()] >= k && B[d.back()] >= B[i]
B[future id] - B[i] >= k too
so no need to keep B[d.back()]
What is the purpose of second while loop?
To keep B[D[i]]
increasing in the deque.
Why keep the deque increase?
If B[i] <= B[d.back()]
and moreover we already know that i > d.back()
, it means that compared with d.back()
,B[i]
can help us make the subarray length shorter and sum bigger. So no need to keep d.back()
in our deque.
1 int shortestSubarray(vector<int> A, int K) { 2 int N = A.size(), res = N + 1; 3 deque<int> d; 4 for (int i = 0; i < N; i++) { 5 if (i > 0) 6 A[i] += A[i - 1]; 7 if (A[i] >= K) 8 res = min(res, i + 1); 9 while (d.size() > 0 && A[i] - A[d.front()] >= K) 10 res = min(res, i - d.front()), d.pop_front(); 11 while (d.size() > 0 && A[i] <= A[d.back()]) 12 d.pop_back(); 13 d.push_back(i); 14 } 15 return res <= N ? res : -1; 16 }
https://leetcode.com/problems/minimum-window-substring/
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC"
Note:
""
.
Variation 1:
1 class Solution { 2 public: 3 string minWindow(string s, string t) { 4 unordered_map<char, int> m; 5 for (char c : t) { 6 m[c]++; 7 } 8 int sz = m.size(); 9 string res; 10 for (int i = 0, j = 0, cnt = 0; i < s.size(); ++i) { 11 if (m[s[i]] == 1) 12 cnt++; 13 m[s[i]]--; 14 while (m[s[j]] < 0) { 15 m[s[j++]]++; 16 } 17 18 if (cnt == sz) { 19 if (res.empty() || res.size() > i - j + 1) 20 res = s.substr(j, i - j + 1); 21 } 22 } 23 return res; 24 } 25 };
Variation 2:
1 class Solution { 2 public: 3 string minWindow(string s, string t) { 4 vector<int> v(128, 0); 5 for (char c : t) 6 v[c]++; 7 int start = 0, end = 0, head = 0, counter = t.size (), len = INT_MAX; 8 while (end < s.size ()) 9 { 10 if (v[s[end++]]-- > 0) counter--; 11 while (counter == 0) 12 { 13 if (end - start < len) 14 { 15 len = end - start; 16 head = start; 17 } 18 if (v[s[start++]]++ == 0) counter++; 19 } 20 } 21 return len == INT_MAX ? "" : s.substr (head, len); 22 } 23 };
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: The 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.
Variation 1:
1 #define vi vector<int> 2 class Solution { 3 public: 4 int lengthOfLongestSubstring(string s) { 5 const int n = s.size(); 6 vi v(128, 0); 7 int i = 0, j = 0, res = 0; 8 while (i < n) { 9 v[s[i]]++; 10 while (j < n && v[s[i]] > 1) 11 v[s[j++]] --; 12 res = max(res, j - i + 1); 13 i++; 14 } 15 return res; 16 } 17 };
Variation 2:
1 int recommend(string s) 2 { 3 vector<int> v(128, 0); 4 int start = 0, end = 0, d = 0, counter = 0; 5 while (end < s.size()) { 6 if (v[s[end++]]++ > 0) 7 counter++; 8 while (counter > 0) { 9 if (v[s[start++]]-- > 1) 10 counter--; 11 } 12 d = max(d, end - start); 13 } 14 return d; 15 }
Given a string s , find the length of the longest substring t that contains at most 2 distinct characters.
Example 1:
Input: "eceba" Output: 3 Explanation: tis "ece" which its length is 3.
Example 2:
Input: "ccaabbb" Output: 5 Explanation: tis "aabbb" which its length is 5.
1 int lengthOfLongestSubstringTwoDistinct(string s) { 2 vector<int> map(128, 0); 3 int counter=0, begin=0, end=0, d=0; 4 while(end<s.size()){ 5 if(map[s[end++]]++==0) counter++; 6 while(counter>2) if(map[s[begin++]]--==1) counter--; 7 d=max(d, end-begin); 8 } 9 return d; 10 }
https://leetcode.com/problems/minimum-window-substring/
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC"
Note:
""
.Variation 1:
1 class Solution { 2 public: 3 string minWindow(string s, string t) { 4 unordered_map<char, int> m; 5 for (char c : t) { 6 m[c]++; 7 } 8 int sz = m.size(); 9 string res; 10 for (int i = 0, j = 0, cnt = 0; i < s.size(); ++i) { 11 if (m[s[i]] == 1) 12 cnt++; 13 m[s[i]]--; 14 while (m[s[j]] < 0) { 15 m[s[j++]]++; 16 } 17 18 if (cnt == sz) { 19 if (res.empty() || res.size() > i - j + 1) 20 res = s.substr(j, i - j + 1); 21 } 22 } 23 return res; 24 } 25 };
Variation 2:
1 string minWindow(string s, string t) { 2 vector<int> map(128,0); 3 for(auto c: t) 4 map[c]++; 5 int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0; 6 while(end<s.size()){ 7 if(map[s[end++]]-->0) 8 counter--; //in t 9 while(counter==0){ //valid 10 if(end-begin<d) 11 d=end-(head=begin); 12 if(map[s[begin++]]++==0) 13 counter++; //make it invalid 14 } 15 } 16 return d==INT_MAX? "":s.substr(head, d); 17 }
[Leetcode] Sliding Window Summary
标签:char ini cab cond bigger red tput ble push
原文地址:https://www.cnblogs.com/goldenticket/p/11986499.html