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.heights=[20, 5, 14, 9, 11] → 5
The whole sequence works: the jumps 15, 9, 5, 2 only shrink.
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.
heights.length ≤ 10⁴heights[i] ≤ 300heights=[20, 5, 14, 9, 11]
5
Run checks the first 3 tests in your browser. Submit checks all 114 tests on the server, hidden ones included.