Subarrays with K Different Integers
Given an arrayAof positive integers, call a (contiguous, not necessarily distinct) subarray ofA_good_if the number of different integers in that subarray is exactlyK.
(For example,[1,2,3,1,2]has3different integers:1,2, and3.)
Return the number of good subarrays ofA.
Example
Example 1:
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].Example 2:
Input:
A = [1,2,1,3,4], K = 3
Output: 3
Explanation:
Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].Note
关键是求刚好K个不同的,参考之前的最多k个不同
子串最多有k个不同的,满足条件的最大长度的窗口长度为就为个数
Code
Last updated