Ctrl K

Longest Downhill Route

Hard
Platforms stand in a row, and platform i has height heights[i]. You may jump from platform i to platform j when all of the following hold: - j is at most reach positions away: 0 < |i - j| ≤ reach, and j lies inside the row; - heights[j] < heights[i], so jumps only go down; - every platform strictly between i and j is also lower than heights[i]; otherwise it blocks the jump. You pick any starting platform and jump as many times as you like. Return the largest number of platforms a single route can visit, counting the starting one.
Example 1
heights=[5, 1, 3, 2, 7, 4], reach=2 → 3

Start at index 4 (height 7): 4 → 2 (height 3, and the only cell in between has 2 < 7) → 3 (height 2). Another route of three is 4 → 2 → 1. No route visits more than three platforms.

Example 2
heights=[2, 2, 2], reach=2 → 1

Jumps must go strictly down and all platforms are equally high, so only one platform is visited.

Example 3
heights=[1, 2, 3, 4], reach=1 → 4
Constraints
  • 1 ≤ len(heights) ≤ 1000
  • 1 ≤ heights[i] ≤ 10⁵
  • 1 ≤ reach ≤ len(heights)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoxes Inside Boxes HardStrict Climb From Spare Parts HardBest-Paying Set of Gigs HardBiggest Number Divisible by Three HardBest Order for a Tasting Menu Hard
loading Python…
Input
heights=[5, 1, 3, 2, 7, 4], reach=2
Expected
3

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