본문 바로가기

IT/코딩테스트

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

728x90

https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times

class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
    
        count = 0

        maximum = max(nums)
        if nums.count(maximum) < k:
            return count

        maximum_list = []
        f = 0
        for i in range(len(nums)):
            if nums[i] == maximum:
                maximum_list.append(i)
            if len(maximum_list) >= k:
                count += maximum_list[len(maximum_list) - k] + 1

        return count
728x90