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.
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个不同
numAtMostK(nums, k) - numAtMostK(nums, k - 1);
子串最多有k个不同的,满足条件的最大长度的窗口长度为就为个数
Code
classSolution {publicintsubarraysWithKDistinct(int[] nums,int k) {returnnumAtMostK(nums, k)-numAtMostK(nums, k -1); }privateintnumAtMostK(int[] nums,int k) {Map<Integer,Integer> map =newHashMap<>();int j =0;int len =nums.length;int count =0;int res =0;for (int i =0; i < len; i++) {while (j < len && count <= k) {map.put(nums[j],map.getOrDefault(nums[j],0) +1);if (map.get(nums[j]) ==1) {if (count == k) {map.put(nums[j],map.get(nums[j]) -1);break; } count++; } j++; } res += j - i;if (map.getOrDefault(nums[i],0) ==1) { count--; }map.put(nums[i],map.get(nums[i]) -1); }return res; }}