Leikou problem brushing notes: 992. K sub-arrays of different integers (sliding window method, easy-to-understand code, complete solution code and comments) (violent solution, the result of python timeout...)

topic:

992, K sub-arrays of different integers

Given an array of positive integers A, if the number of different integers in a sub-array of A happens to be K, then this continuous, not necessarily independent sub-array of A is called a good sub-array.

(For example, there are 3 different integers in [1,2,3,1,2]: 1, 2, and 3.)

Returns the number of good sub-arrays in A.

Example 1:

Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: A sub-array consisting of exactly 2 different integers: [1,2], [2,1], [1, 2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].

Example 2:

Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: A sub-array consisting of exactly 3 different integers: [1,2,1,3], [2,1, 3], [1,3,4].

prompt:

1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length

Problem solution ideas:

We can make the following conversion: The number of sub-arrays with at least K different integers-the number of sub-arrays with at least K+1 different integers

Solve sub-problems:

(1) Enumerate i, find the number of sub-arrays starting with i and at least K different integers
(2) If we find a qualified j such that [i,j] is a sub-array that meets the conditions, Then [i,j],[i,j+1],[i,j+2],...,[i,n-1] are all sub-arrays that meet the conditions.
(3) After that, we shrink the window until the array in the window does not meet the conditions, and then re-enlarge the window and return to the previous step.

Problem solution python code:

class Solution:
    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
        def helper(A,K):
            n = len(A)
            i = 0
            res = 0
            ########################## 
            # 窗口内需要维护的变量
            diff_nums = 0
            counter = collections.defaultdict(int)
            ##########################
            for j in range(n):
                # 扩大窗口
                counter[A[j]] += 1
                if counter[A[j]] == 1:
                    diff_nums += 1

                # 如果找到以i开头满足条件的子数组了,就更新答案并缩小窗口
                while diff_nums > K:
                    res += n-j
                    counter[A[i]] -= 1
                    if counter[A[i]] == 0:
                        diff_nums -= 1
                    i += 1
            return res
        
        return -helper(A,K) + helper(A,K-1)

Insert picture description here

Author: MiloMusiala
link: https://leetcode-cn.com/problems/subarrays-with-k-different-integers/solution/python-dong-hua-tong-xiang-shuang-zhi-zh-57ym/
Source: force LeetCode (LeetCode) https://leetcode-cn.com/problems/subarrays-with-k-different-integers/

Violent solution ideas:

Get all the sub-arrays of A, determine whether its different integers are equal to K while obtaining each sub-array, and finally return the number of sub-arrays that meet the conditions.

Simple and rude, but unfortunately it's overtime. . .

Can be used in actual projects, for reference only

Violent solution python code:

class Solution:
    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
        ans = 0
        n = len(A)
        # x + 1 表示子串的长度
        for x in range(n):
            # i 表示滑窗长度
            for i in range(x+1,n+1):
                # if len(set(A[x:i]))==K:
                #     ans += 1
                if len(set(A[x:i]))==K:
                    ans += 1
        return ans

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/113771127