-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
31 lines (31 loc) · 798 Bytes
/
Solution.cs
File metadata and controls
31 lines (31 loc) · 798 Bytes
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
public class Solution
{
public int CountCompleteSubarrays(int[] nums)
{
HashSet<int> set = [.. nums];
int distinct = set.Count;
Dictionary<int, int> map = [];
int ret = 0;
int n = nums.Length;
int left = 0;
for (int right = 0; right < n; right++)
{
map.TryAdd(nums[right], 0);
map[nums[right]]++;
while (left <= right && map.Keys.Count == distinct)
{
ret += n - right;
if (map[nums[left]] == 1)
{
map.Remove(nums[left]);
}
else
{
map[nums[left]]--;
}
left++;
}
}
return ret;
}
}