Skip to content
LC-2962 Medium LeetCode

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

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 63% Topics: Array, Sliding Window
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# two pointers, sliding window
class Solution(object):
    def countSubarrays(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        mx = max(nums)
        result = left = cnt = 0
        for right in xrange(len(nums)):
            cnt += int(nums[right] == mx)
            while cnt == k:
                cnt -= int(nums[left] == mx)
                left += 1
            result += left
        return result


# Time:  O(n)
# Space: O(1)
# two pointers, sliding window
class Solution2(object):
    def countSubarrays(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        mx = max(nums)
        result = (len(nums)+1)*len(nums)//2
        left = cnt = 0
        for right in xrange(len(nums)):
            cnt += int(nums[right] == mx)
            while cnt == k:
                cnt -= int(nums[left] == mx)
                left += 1
            result -= right-left+1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT