Count the number of contiguous subarrays in which the maximum value of the whole array appears at least k times.
Problem
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.
Clarification
The maximum element refers to the largest value present anywhere in the entire array, not the maximum of each subarray.
Input Format
- An integer array
nums - An integer
k
The exact platform function signature may vary, but the logical inputs are the same.
Output Format
Return an integer representing the number of contiguous subarrays whose global maximum element appears at least k times.
Constraints
1 <= nums.length1 <= knums[i]are integers- The answer may be larger than 32-bit signed integer range, so use a 64-bit integer type if needed.
Example 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
Unlock deeper context for this problem
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.