Skip to content

Commit ec2ea7c

Browse files
authored
Create 2537. Count the Number of Good Subarrays (#769)
2 parents 9cd2c5a + be8d5d7 commit ec2ea7c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
long long countGood(vector<int>& nums, int k) {
4+
long long int l = 0, r= 0;
5+
long long int ans = 0, count = 0;
6+
unordered_map<int, int> mp;
7+
int n = nums.size();
8+
while(r < n){
9+
mp[nums[r]]++;
10+
count += (mp[nums[r]]-1);
11+
while(l < n && (count- (mp[nums[l]]-1)) >= k){
12+
mp[nums[l]]--;
13+
count -= mp[nums[l]];
14+
l+=1;
15+
16+
}
17+
if(count >= k ){
18+
ans += (l+1);
19+
}
20+
r++;
21+
22+
}
23+
return ans;
24+
}
25+
};

0 commit comments

Comments
 (0)