-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2799.cpp
More file actions
25 lines (24 loc) · 742 Bytes
/
2799.cpp
File metadata and controls
25 lines (24 loc) · 742 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
class Solution {
public:
int countCompleteSubarrays(vector<int>& nums) {
unordered_set<int> st(nums.begin(), nums.end());
int diff = st.size();
int res = 0;
int right = 0;
int n = nums.size();
unordered_map<int, int> mp;
int currentDiff = 0;
// [left, right]
for (int left = 0; left < n; ++left) {
while (currentDiff < diff && right < n) {
mp[nums[right]]++;
if (mp[nums[right]] == 1) currentDiff++;
right++;
}
if (currentDiff == diff) res += (n - right + 1);
mp[nums[left]]--;
if (mp[nums[left]] == 0) currentDiff--;
}
return res;
}
};