2962.cpp (425B)
1 class Solution { 2 public: 3 long long countSubarrays(const vector<int> &nums, int k) const { 4 int maxi = *max_element(begin(nums), end(nums)); 5 long long res = 0; 6 7 for (int i = 0, j = 0, crnt = 0; j < size(nums); j++) { 8 if (nums[j] == maxi) crnt++; 9 while (crnt >= k) 10 if (nums[i++] == maxi) crnt--; 11 res += i; 12 } 13 14 return res; 15 } 16 };