https://leetcode.com/problems/count-complete-subarrays-by-the-number-of-distinct-integers/
- Sliding Window
Count elements with distinct count equal to total distinct in array.
O(n)
O(n)
import java.util.*;
class Solution {
public int countCompleteSubarrays(int[] nums) {
Set<Integer> distinct = new HashSet<>();
for (int n : nums) distinct.add(n);
int target = distinct.size(), ans = 0;
for (int left = 0; left < nums.length; left++) {
Set<Integer> window = new HashSet<>();
for (int right = left; right < nums.length; right++) {
window.add(nums[right]);
if (window.size() == target) ans++;
}
}
return ans;
}
}