You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class44;
import java.util.HashMap;
public class Problem_0992_SubarraysWithKDifferentIntegers {
// nums 数组题目规定nums中的数字不会超过nums的长度
// [ ]长度为50~5
public static int subarraysWithKDistinct1(int[] nums, int k) {
int n = nums.length;
// k-1种数的窗口词频统计
int[] lessCounts = new int[n + 1];
// k种数的窗口词频统计
int[] equalCounts = new int[n + 1];
int lessLeft = 0;
int equalLeft = 0;
int lessKinds = 0;
int equalKinds = 0;
int ans = 0;
for (int r = 0; r < n; r++) {
// 当前刚来到r位置
if (lessCounts[nums[r]] == 0) {
lessKinds++;
}
if (equalCounts[nums[r]] == 0) {
equalKinds++;
}
lessCounts[nums[r]]++;
equalCounts[nums[r]]++;
while (lessKinds == k) {
if (lessCounts[nums[lessLeft]] == 1) {
lessKinds--;
}
lessCounts[nums[lessLeft++]]--;
}
while (equalKinds > k) {
if (equalCounts[nums[equalLeft]] == 1) {
equalKinds--;
}
equalCounts[nums[equalLeft++]]--;
}
ans += lessLeft - equalLeft;
}
return ans;
}
public static int subarraysWithKDistinct2(int[] arr, int k) {
return numsMostK(arr, k) - numsMostK(arr, k - 1);
}
public static int numsMostK(int[] arr, int k) {
int i = 0, res = 0;
HashMap<Integer, Integer> count = new HashMap<>();
for (int j = 0; j < arr.length; ++j) {
if (count.getOrDefault(arr[j], 0) == 0) {
k--;
}
count.put(arr[j], count.getOrDefault(arr[j], 0) + 1);
while (k < 0) {
count.put(arr[i], count.get(arr[i]) - 1);
if (count.get(arr[i]) == 0) {
k++;
}
i++;
}
res += j - i + 1;
}
return res;
}
}