2962. Count Subarrays Where Max Element Appears at Least K Times
廢廢如我
只想到慢慢用map記index
都比那些sliding的慢 一生就這樣了
long long countSubarrays(vector<int>& nums, int k) {
unordered_map<int,int> mp;
int maxi = *max_element(nums.begin(), nums.end());
int cnt_now = 0;
long long ans = 0;
for(int i=0; i<nums.size(); i++)
{
if(nums[i] == maxi)
{
cnt_now += 1;
mp[cnt_now] = i;
}
if(mp.find(cnt_now-k+1) != mp.end())
{
ans += (mp[cnt_now-k+1]+1);
}
}
return ans;
}