Count Complete Subarrays in an Array (LeetCode 2799): Solution and Explanation
Count the subarrays that contain every distinct value of the whole array at least once.
Intuition
First count k, the number of distinct values in the full array. A subarray is complete exactly when it holds all k of them.
Completeness only grows as a subarray gets longer. If [l..r] is complete, then pulling the start further left keeps every value inside, so [l-1..r], [l-2..r] and so on are complete too.
So for a fixed right end r, the complete subarrays are the ones whose start is before some boundary. If we find that boundary, the count for r is just its index. A sliding window finds it as r moves right.
Approach: Shrink until incomplete, then count starts
- Compute
k = len(set(nums))and keep a count per value plushave, the number of distinct values in the window. - For each
right, addnums[right]; if its count becomes 1, increasehave. - While
have == k, removenums[left], decreasinghaveif its count drops to 0, and moveleftforward. - After the loop, the window [left..right] is missing one value, while [left-1..right] was complete. So starts 0..left-1 are all complete: add
leftto the answer. - Return the total.
Step-by-step example
nums = [1,3,1,2,2]
- k = 3 (values 1, 2, 3).
- right = 0, 1, 2: the window holds at most 1 and 3, never 3 distinct values, so left stays 0 and nothing is added.
- right = 3 (value 2): the window [0..3] has all three. Remove 1 at index 0, still complete because index 2 has a 1. Remove 3 at index 1, now 3 is missing. left = 2, add 2.
- right = 4 (value 2): the window [2..4] holds 1 and 2 only. left stays 2, add 2.
- Total = 2 + 2 = 4.
Code: Python, Java, C++
1class Solution:
2 def countCompleteSubarrays(self, nums: List[int]) -> int:
3 k = len(set(nums))
4 count = [0] * 2001
5 have = 0
6 left = 0
7 res = 0
8 for x in nums:
9 count[x] += 1
10 if count[x] == 1:
11 have += 1
12 while have == k:
13 count[nums[left]] -= 1
14 if count[nums[left]] == 0:
15 have -= 1
16 left += 1
17 res += left
18 return res1class Solution {
2 public int countCompleteSubarrays(int[] nums) {
3 boolean[] seen = new boolean[2001];
4 int k = 0;
5 for (int x : nums) {
6 if (!seen[x]) {
7 seen[x] = true;
8 k++;
9 }
10 }
11 int[] count = new int[2001];
12 int have = 0, left = 0, res = 0;
13 for (int x : nums) {
14 count[x]++;
15 if (count[x] == 1) {
16 have++;
17 }
18 while (have == k) {
19 count[nums[left]]--;
20 if (count[nums[left]] == 0) {
21 have--;
22 }
23 left++;
24 }
25 res += left;
26 }
27 return res;
28 }
29}1class Solution {
2public:
3 int countCompleteSubarrays(vector<int>& nums) {
4 int k = unordered_set<int>(nums.begin(), nums.end()).size();
5 vector<int> count(2001, 0);
6 int have = 0, left = 0, res = 0;
7 for (int x : nums) {
8 count[x]++;
9 if (count[x] == 1) {
10 have++;
11 }
12 while (have == k) {
13 count[nums[left]]--;
14 if (count[nums[left]] == 0) {
15 have--;
16 }
17 left++;
18 }
19 res += left;
20 }
21 return res;
22 }
23};Time and space complexity
O(n)
Each index is added once and removed at most once, and every step is constant work.
O(V)
A count array over the value range (V = 2000), or a hash map with at most k entries.
Other approaches
Try every start (time O(n^2), space O(k))
For each start, extend right with a set until it holds k values; then every longer subarray from that start is complete too, so add n - r. With n up to 1000 this is fast enough.
At most k minus at most k - 1 (time O(n), space O(k))
The number of subarrays with exactly k distinct values equals atMost(k) - atMost(k - 1), the trick from Subarrays with K Different Integers. Since no subarray can have more than k, exactly k is the answer.
Common mistakes
- Adding
right - left + 1like in many window problems. Here the complete subarrays are the ones starting beforeleft, so the amount to add isleft. - Forgetting the case k = 1. Every subarray is complete, and the window logic handles it: left ends at right + 1 each time, giving 1 + 2 + ... + n.
- Decreasing
haveon every removal instead of only when a value's count reaches 0. - Comparing against the window's own distinct count instead of the whole array's k.
Frequently asked questions
Why does adding left count the right subarrays?
The while loop stops the first time the window loses a value, so [left-1..right] was complete and [left..right] is not. Every start from 0 to left-1 contains [left-1..right] and is complete; every start from left onward is inside [left..right] and is not.
Can left move backward?
No. When right grows, a window that was incomplete can only become complete, never the reverse, so the boundary only moves forward.
Does the answer fit in an int?
Yes. With n at most 1000 there are at most 500,500 subarrays.