<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p>

<p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p>

<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,2,3,3], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,4,2,1], k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> No subarray contains the element 4 at least 3 times.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

from collections import Counter
import numpy as np

class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        max_num = np.max(nums)
        print(f'max is: {max_num}')
        max_num_indices = np.where(nums == max_num)[0]
        result = 0
        left_bound = -1 # the left boundary should be non-decreasing
        
        for i, max_num_index in enumerate(max_num_indices):
            if i < k-1:
                continue
            left_bound = max_num_indices[i - (k-1)]
            result += left_bound + 1
            if i < len(max_num_indices)-1:
                result += (left_bound + 1)*(max_num_indices[i+1] - max_num_indices[i] - 1)

        if max_num_indices[-1] != len(nums)-1 and left_bound != -1: # if left_bound is still -1 that means we don't even have enough max_num in the whole array
            result += (left_bound + 1)*(len(nums) - max_num_indices[-1] - 1)
        return result
1038