989. Max Appears K Times

Medium3.5 points estimatedArraySliding Window3 hints

A weather station records daily readings in nums. Let M be the largest reading in the entire array. Count the contiguous stretches of days (subarrays) in which the value M shows up at least k times.

Two stretches are different if their start or end days differ, even when their contents match. Return the count as a 64-bit integer; it can be as large as about 5 * 10^9, so it may not fit in a 32-bit int.

Example 1

Input:
nums = [2,7,3,7,1,7], k = 2
Output:
8
Explanation:

The largest value is 7, at indices 1, 3, 5. A subarray needs at least two of them: starts 0 or 1 with ends 3..5 give 6, and starts 2 or 3 ending at 5 give 2 more, total 8.

Example 2

Input:
nums = [4,4,4], k = 3
Output:
1
Explanation:

The largest value is 4 and it must appear three times, so only the whole array works: 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ 105

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 800 msC++ 200 msJava 400 msJavaScript 400 msTypeScript 400 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…