Skip to the code
+25 XP

#273 Longest Calm Stretch

medium

A video player logs the bitrate of each second in rates. A stretch of consecutive seconds is "calm" if its highest and lowest bitrates differ by at most limit.

Write longestCalm(rates, limit) that returns the length of the longest calm stretch. A single second is always calm.

Example 1

Input: rates = [8,2,4,7], limit = 4
Output: 2

[2, 4] and [4, 7] differ by at most 4; any longer stretch includes a bigger gap.

Example 2

Input: rates = [10,1,2,4,7,2], limit = 5
Output: 4

[2, 4, 7, 2] has max 7 and min 2, a difference of 5.

Constraints

  • 1 <= rates.length <= 2 * 10^5
  • 1 <= rates[i] <= 10^9
  • 0 <= limit <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).