Skip to content

Commit df97205

Browse files
authored
Create 4 July Subarrays With At Most K Distinct Integers
1 parent f3b67a1 commit df97205

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int countAtMostK(vector<int> &arr, int k) {
4+
unordered_map<int,int>mp;
5+
int n = arr.size(), j = 0, ans = 0;
6+
for (int i=0; i<n; i++){
7+
mp[arr[i]]+=1;
8+
while (j <= i && mp.size() > k){
9+
mp[arr[j]]-=1;
10+
if (mp[arr[j]] == 0) mp.erase(arr[j]);
11+
j++;
12+
}
13+
ans += (i - j + 1);
14+
}
15+
return ans;
16+
}
17+
};

0 commit comments

Comments
 (0)