Count the number of contiguous subarrays in which the maximum value of the whole array appears at least k times.
Given an integer array nums and an integer k, count how many contiguous subarrays contain the maximum element of nums at least k times.
A subarray is a non-empty contiguous segment of the array.
You need to return the total number of such subarrays.
The maximum element refers to the largest value present anywhere in the entire array, not the maximum of each subarray.
numskThe exact platform function signature may vary, but the logical inputs are the same.
Return an integer representing the number of contiguous subarrays whose global maximum element appears at least k times.
1 <= nums.length1 <= knums[i] are integersExample 1
Input
nums = [1,3,2,3,3], k = 2
Output
6
Explanation
The global maximum is 3. The subarrays with at least two 3s are [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3], and [3,3].
Example 2
Input
nums = [5,5,5], k = 3
Output
1
Explanation
Only the whole array [5,5,5] contains the maximum value 3 times.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.