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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
// 2799. Count Complete Subarrays in an Array
#include "leetcode.h"
/*
* 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.
*/
int countCompleteSubarrays(int *nums, int numsSize) {
int elem[2001] = {0}, distinct = 0, ans = 0;
for (int i = 0; i < numsSize; i++) {
if (!elem[nums[i]])
distinct++;
elem[nums[i]]++;
}
int low = 0, high = 0, curr = 0;
memset(elem, 0, sizeof(elem));
while (curr < distinct) {
if (!elem[nums[high]])
curr++;
elem[nums[high]]++;
high++;
}
ans += numsSize - high + 1;
while (low < numsSize) {
elem[nums[low]]--;
if (!elem[nums[low]])
curr--;
while (high < numsSize && curr < distinct) {
if (!elem[nums[high]])
curr++;
elem[nums[high]]++;
high++;
}
if (curr < distinct)
break;
ans += numsSize - high + 1;
low++;
}
return ans;
}
int main() {
int n1[] = {1, 3, 1, 2, 2}, n2[] = {5, 5, 5, 5};
printf("%d\n", countCompleteSubarrays(n1, ARRAY_SIZE(n1))); // expect: 4
printf("%d\n", countCompleteSubarrays(n2, ARRAY_SIZE(n2))); // expect: 10
}
|