Leetcode-904+992-fruit into a basket + k sub-arrays of different integers

904

Title description

Insert picture description here
Understanding the meaning of the question is actually:
we have to find the largest sub-array containing two different integers in the specified array

Idea sliding window

The same classic approach:

  • Continue to explore the right boundary of the window until it reaches the array boundary
  • When the right boundary encounters a special situation, change the left boundary to meet the requirements
  • Update solution every time

The specific ideas are not written, it is not difficult

class Solution {
    
    
public:
    int totalFruit(vector<int>& tree) {
    
    
        int left = 0, right = 0, res = 0;
        int len = tree.size();
        unordered_map<int, int> m;
        int target = 0;
        while(right < len) {
    
    
            if(m[tree[right]] == 0) {
    
    
                target++;
            }
            m[tree[right]]++;
            while(target > 2) {
    
    
                m[tree[left]]--;
                if(m[tree[left]] == 0) {
    
    
                    target--;
                    m.erase(tree[left]);
                }
                left++;
            }
            res = max(res, right - left + 1);
            right++;
        }
        return res;
    }
};

Time complexity O(n)
Space complexity O(n)

992

Title description

Insert picture description here

Ideas

Big guy's clear thoughts The
problem is that they can't come up with ideas. After reading the big guy's ideas, the code implementation is not difficult.
Idea: The number of sub-arrays composed of up to K different integers in A-The number of sub-arrays composed of up to K-1 different integers in A is the answer to the problem.
Then it is converted to solve the number of sub-arrays composed of at most n different integers.
Here, just modify one sentence of the 904 code.

 res = max(res, right - left + 1);

To

 res += right - left + 1;	

When we look for a new right boundary elements, get a new sub-array, and the number of its new sub-array is right - left + 1 Ge
chiefs had somehow move the map, and all of you to see it understood. (Too much realism) Go
directly to the code

class Solution {
    
    
public:
    //最终结果为A中由最多 K 个不同整数组成的子数组的个数
    //减去A中由最多 K - 1个不同整数组成的子数组的个数
    int subarraysWithKDistinct(vector<int>& A, int K) {
    
    
        return maxSubarrayCount(A, K) - maxSubarrayCount(A, K - 1);
    }
    //A中由最多 K 个不同整数组成的子数组的个数
    int maxSubarrayCount(vector<int> A, int k) {
    
    
        unordered_map<int, int> m;
        int left = 0, right = 0;
        int len = A.size();
        int target = 0;
        int res = 0;
        while(right < len) {
    
    
            if(m[A[right]] == 0) {
    
    
                target++;
            }
            m[A[right]]++;
            while(target > k) {
    
    
                m[A[left]]--;
                if(m[A[left]] == 0) {
    
    
                    target--;
                }
                left ++;
            }
            res += right - left + 1;
            right++;
        }
        return res; 
    }
};

Space complexity O(n)
Time complexity O(n)

Guess you like

Origin blog.csdn.net/qq_42883222/article/details/113781390