We need to count subarrays where the maximum element of the entire array appears at least k times. The simplest approach is to check every possible subarray by trying all starting and ending positions, counting occurrences of the maximum element in each subarray.
i from 0 to n-1:j from i to n-1:nums[j] equals the max element, increment the counter.k, this subarray is valid; increment the result.Instead of checking all subarrays, we can use a sliding window. For each right endpoint, we find the smallest left endpoint such that the window contains exactly k occurrences of the max element with the max element at position l. All positions from 0 to l can serve as left endpoints for valid subarrays ending at r.
l and r, both starting at 0.nums[r] is the max element, increment the count.k, or while count equals k and the left element is not the max (to find the rightmost valid left position).k, add (l + 1) to the result, representing all valid starting positions.class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
max_n, max_cnt = max(nums), 0
l = 0
res = 0
for r in range(len(nums)):
if nums[r] == max_n:
max_cnt += 1
while max_cnt > k or (l <= r and max_cnt == k and nums[l] != max_n):
if nums[l] == max_n:
max_cnt -= 1
l += 1
if max_cnt == k:
res += l + 1
return resWe can simplify the sliding window by counting subarrays with fewer than k occurrences and subtracting from total, or equivalently, counting invalid prefixes. For each right endpoint, we maintain a window that has exactly k occurrences of the max element, then shrink it until we have fewer than k. The left pointer position tells us how many valid subarrays end at this right position.
nums[r] is the max element, increment the count.k:nums[l] is the max element, decrement the count.l to the right.l to the result (representing all valid starting positions from 0 to l-1).We can collect all indices where the max element appears and then use combinatorics. For each window of k consecutive max element positions, the number of valid subarrays can be computed by multiplying the number of possible left endpoints (positions before the first max in the window) by the number of possible right endpoints (positions from the last max to the end of the array).
-1 at the beginning of the index list (as a sentinel for computing gaps).k consecutive indices in the list:class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
n = len(nums)
max_n = max(nums)
max_indexes = [-1]
for i, num in enumerate(nums):
if num == max_n:
max_indexes.append(i)
res = 0
for i in range(1, len(max_indexes) - k + 1):
cur = (max_indexes[i] - max_indexes[i - 1])
cur *= (n - max_indexes[i + k - 1])
res += cur
return resWe maintain a sliding window containing exactly k indices of the max element. As we scan through the array, whenever we encounter the max element, we add its index to a queue. When the queue has more than k indices, we remove the oldest one. Whenever we have exactly k max element indices in our window, all positions from 0 to the first index in the queue are valid starting points for subarrays ending at the current position.
i in the array:nums[i] equals the max element, add i to the queue.k, remove the front element.k, add (front index + 1) to the result.class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
max_n = max(nums)
max_indexes = deque()
res = 0
for i, num in enumerate(nums):
if num == max_n:
max_indexes.append(i)
if len(max_indexes) > k:
max_indexes.popleft()
if len(max_indexes) == k:
res += max_indexes[0] + 1
return res