1. 程式人生 > 實用技巧 >滑動視窗型別

滑動視窗型別

子集型別

先看一題

(leetcode 76) 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).
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

string minWindow(string s, string t) {
    vector<int> map(128,0);
    for(auto c: t) map[c]++;
    int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0;
    while(end<s.size()){
        if(map[s[end++]]-->0) counter--; //in t
        while(counter==0){ //valid
            if(end-begin<d)  d=end-(head=begin);
            if(map[s[begin++]]++==0) counter++;  //make it invalid
        }  
    }
    return d==INT_MAX? "":s.substr(head, d);
}

對於子集的型別,模板如下

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;
}

最長子序列之最多兩個不同字元

(leetcode 159)Longest Substring with At Most Two Distinct Characters
  Given a string s , find the length of the longest substring t that contains at most 2 distinct characters.
Input: "eceba"
Output: 3
Explanation: tis "ece" which its length is 3.

int lengthOfLongestSubstringTwoDistinct(string s) {
    vector<int> map(128, 0);
    int counter=0, begin=0, end=0, d=0; 
    while(end<s.size()){
        if(map[s[end++]]++==0) counter++;
        while(counter>2) if(map[s[begin++]]--==1) counter--;
        d=max(d, end-begin);
    }
    return d;
}

最長子序列之無重複字元

(leetcode 3)Longest Substring Without Repeating Characters
  Given a string, find the length of the longest substring without repeating characters.
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

int lengthOfLongestSubstring(string s) {
    vector<int> map(128,0);
    int counter=0, begin=0, end=0, d=0; 
    while(end<s.size()){
        if(map[s[end++]]++>0) counter++; 
        while(counter>0) if(map[s[begin++]]-->1) counter--;
        d=max(d, end-begin); //while valid, update d
    }
    return d;
}

子串包含另一子串的排列

(leetcode 567)Permutation in String
  Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string.
Input: s1 = "ab" s2 = "eidbaooo"
Output: True
Explanation: s2 contains one permutation of s1 ("ba").

bool checkInclusion(string s1, string s2) {
    vector<int> map(128, 0);
    int counter=s1.size();
    int begin=0, end=0;
    for(auto c: s1){
        map[c]++;
    }

    while(end<s2.size()){
        if(map[s2[end++]]-->0) counter--;
        if(counter==0) return true;
        if(end-begin==s1.size() && map[s2[begin++]]++>=0) counter++;
    }
    return false;
}

K個不同整數的子序列個數

992. Subarrays with K Different Integers
  Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].

int subarraysWithKDistinct(vector<int>& A, int K) {
    vector<int> map(A.size()+1, 0);
    int begin=0, end=0;
    int res=0;
    int cnt=0;
    int pre_num=0;
    while(end<A.size()){
        if(map[A[end++]]++==0) cnt++;
        if(cnt>K) --map[A[begin++]], cnt--, pre_num=0;
        while(map[A[begin]]>1){
            map[A[begin++]]--;
            pre_num++;
        }
        if(cnt==K) res+=(1+pre_num);
    }
    return res;
}

奇偶個數

使0、1陣列都為1的最少翻轉次數

995. Minimum Number of K Consecutive Bit Flips
  In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.
Return the minimum number of K-bit flips required so that there is no 0 in the array. If it is not possible, return -1.
Input: A = [0,1,0], K = 1
Output: 2
Explanation: Flip A[0], then flip A[2].

int minKBitFlips(vector<int>& A, int K) {
    int n=A.size();
    int cur=0, res=0;
    for(int i=0;i<n;i++){
        if(i>=K&&A[i-K]>1){
            cur--;
            A[i-K]-=2;
        }
        if(cur%2==A[i]){
            if(i+K>n) return -1;
            cur++;
            A[i]+=2;
            res++;
        }
    }
    return res;
}