Skip to content

Commit 96bbadd

Browse files
authored
Create 2962. Count Subarrays Where Max Element Appears at Least K Times1 (#781)
2 parents cee138c + 7e5ceb8 commit 96bbadd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
long long countSubarrays(vector<int>& nums, int k) {
4+
5+
int mx=*max_element(begin(nums),end(nums));
6+
long long ans=0;
7+
8+
int l=0,r=0,n=size(nums);
9+
while(r<n){
10+
k-=(nums[r++]==mx);
11+
while(k==0){
12+
k+=(nums[l++]==mx);
13+
}
14+
ans+=l;
15+
}
16+
return ans;
17+
}
18+
};

0 commit comments

Comments
 (0)