2799. Count Complete Subarrays in an Array
Description
You are given an array nums consisting of positive integers.
We call a subarray of an array complete if the following condition is satisfied:
- The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array.
Return the number of complete subarrays.
A subarray is a contiguous non-empty part of an array.
Example 1:
Input: nums = [1,3,1,2,2] Output: 4 Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2].
Example 2:
Input: nums = [5,5,5,5] Output: 10 Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 2000
Solutions
Solution 1: Hash Table + Enumeration
Thinking
A complete subarray contains every distinct value of the original array. Building a set for every pair of ends is acceptable at \(n\le 1000\).
Compute the global number of distinct values \(cnt\), then for each left end grow a set rightward and count a complete subarray whenever the set size reaches \(cnt\).
First, we use a hash table to count the number of distinct elements in the array, denoted as \(cnt\).
Next, we enumerate the left endpoint index \(i\) of the subarray and maintain a set \(s\) to store the elements in the subarray. Each time we move the right endpoint index \(j\) to the right, we add \(nums[j]\) to the set \(s\) and check whether the size of the set \(s\) equals \(cnt\). If it equals \(cnt\), it means the current subarray is a complete subarray, and we increment the answer by \(1\).
After the enumeration ends, we return the answer.
Time complexity: \(O(n^2)\), Space complexity: \(O(n)\), where \(n\) is the length of the array.
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Solution 2: Hash Table + Two Pointers
Thinking
Solution 1 rebuilds a set from each left end. The number of distinct values is monotone in the right end: once the window is complete, every subarray that keeps this right end and a left end at most \(i\) is complete, which adds \(n-j\). Then advance the left end while updating the counts.
Similar to Solution 1, we can use a hash table to count the number of distinct elements in the array, denoted as \(cnt\).
Next, we use two pointers to maintain a sliding window, where the right endpoint index is \(j\) and the left endpoint index is \(i\).
Each time we fix the left endpoint index \(i\), we move the right endpoint index \(j\) to the right. When the number of distinct elements in the sliding window equals \(cnt\), it means that all subarrays from the left endpoint index \(i\) to the right endpoint index \(j\) and beyond are complete subarrays. We then increment the answer by \(n - j\), where \(n\) is the length of the array. Afterward, we move the left endpoint index \(i\) one step to the right and repeat the process.
Time complexity: \(O(n)\), Space complexity: \(O(n)\), where \(n\) is the length of the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |