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

Обновлено: 2024-03-31
1 мин
[]

Содержание

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