How leetcode returns a subarray of K different integers
This article will explain in detail how leetcode returns a subarray of K different integers. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Given a positive integer array A, if the number of different integers in a subarray of A happens to be K, the continuous and not necessarily independent subarray of An is called a good subarray.
(for example, there are three different integers in [1Pert 2, 3pr 1], and 3. )
Returns the number of good subarrays in A.
Example 1:
Output: a = [1, 2, 2, 1, 1, 2, 3], K = 2
Enter: 7
Explanation: a subarray that happens to be composed of two different integers: [1], [2], [1], [2], [2], [2], [2].
Example 2:
Input: a = [1, 2, 1, 3, 4], K = 3
Output: 3
Explanation: a subarray of exactly three different integers: [1], [2], [3], [3].
Tip:
one