heights[i] is the height of stone i. A frog sits on stone 0 and wants to reach the last stone. Each hop goes forward only, from stone i to any stone j > i, and the two heights may differ by at most step: |heights[j] - heights[i]| ≤ step.
The frog loves hopping, so it wants to reach the last stone using as **many** hops as possible. Return that number, or -1 if the last stone cannot be reached at all.heights=[0, 5, 2, 3, 9, 1], step=2 → 3
The best route is 0 → 2 → 3 → 1 (indices 0, 2, 3, 5), three hops. The stones at heights 5 and 9 are too far from the others.
heights=[4, 10, 20], step=5 → -1
No stone is close enough in height to the last one (20), so the answer is −1.
heights=[7, 7, 7, 7], step=0 → 3
len(heights) ≤ 1000heights[i] ≤ 10⁹step ≤ 2·10⁹heights=[0, 5, 2, 3, 9, 1], step=2
3
Run checks the first 3 tests in your browser. Submit checks all 144 tests on the server, hidden ones included.