One surge allowed: hints, no solution

Medium · Sliding window

A transformer logs its load once an hour as an integer. Any reading strictly above a cap is a surge. The operators tolerate one surge inside a review period but not two, since a single surge can be a sensor glitch.

Find the longest run of consecutive readings that contains at most one surge, and return its length. A run with no surge at all is fine too. If the list is empty, return 0. If every reading is a surge, the answer is 1.

Examples

Input
readings = [3, 9, 2, 8, 1, 1, 7, 2], cap = 5
Output
4
Note
The runs 2, 8, 1, 1 and 1, 1, 7, 2 each have one surge and four readings.
Input
readings = [9, 9, 9], cap = 5
Output
1
Note
Every reading is above the cap, and a run can hold only one of them.
Input
readings = [1, 2, 3], cap = 5
Output
3
Note
No reading exceeds the cap, so the whole list is one run.

Constraints

  • 0 <= readings.length <= 300000
  • 0 <= readings[i] <= 100000
  • 0 <= cap <= 100000

Hint 1: a nudge

Which readings in a run actually matter for deciding whether it is allowed?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first