We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9cd2c5a commit be8d5d7Copy full SHA for be8d5d7
2537. Count the Number of Good Subarrays
@@ -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