Ctrl K

Fading Jumps

Medium
You are given an array heights, the heights of steps in order. Pick a subsequence (you may delete elements but not reorder the rest) and look at the jumps between neighbouring picked elements: a jump is the absolute difference of their heights. A subsequence is fading if every jump is no larger than the one before it. Return the length of the longest fading subsequence.
Example 1
heights=[20, 5, 14, 9, 11] → 5

The whole sequence works: the jumps 15, 9, 5, 2 only shrink.

Example 2
heights=[3, 3, 7, 3] → 3

Taking all four fails: the jumps 0, 4, 4 grow. But [3, 7, 3] with jumps 4 and 4, or [3, 3, 3] with jumps 0 and 0, both work, so the length is 3.

Constraints
  • 2 ≤ heights.length ≤ 10⁴
  • 1 ≤ heights[i] ≤ 300
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
heights=[20, 5, 14, 9, 11]
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 114 tests on the server, hidden ones included.