2962. Count Subarrays Where Max Element Appears at Least K Times

Updated: 2024-03-31
1 min read
[]

On This Page

LeetCode problem 2962

class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        mx = max(nums)
        n = len(nums)
        res = cnt = j = 0
        for x in nums:
            while j < n and cnt < k:
                cnt += nums[j] == mx
                j += 1
            if cnt < k:
                break
            res += n - j + 1
            cnt -= x == mx
        return res