Ctrl K

A Frog Crossing the River

Hard
A frog crosses a river on stones. The stone positions are given by a strictly increasing array stones; the frog sits on the first stone (position 0) and wants to reach the last one. It can only jump forward and only onto stones, never into the water. The first jump is exactly 1 unit. If the previous jump had length k, the next one may have length k - 1, k or k + 1 (always positive). Return True if the frog can reach the last stone, and False otherwise.
Example 1
stones=[0, 1, 2, 4, 7] → True

Jumps of 1, 1, 2 and 3: each one differs from the previous by at most 1.

Example 2
stones=[0, 2] → False

The first jump must be exactly 1, and there is no stone at position 1.

Example 3
stones=[0, 1, 3, 4, 8] → False

The frog can reach the stone at 4, but from there it can jump at most 2 units, while 8 is 4 units away.

Constraints
  • 2 ≤ len(stones) ≤ 2000
  • 0 ≤ stones[i] ≤ 2³¹ - 1
  • stones[0] = 0
  • stones is strictly increasing
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWater Between Walls HardBiggest Solid Panel Block HardProfit From Two Trades HardEvery Way to Space the Words HardCrossing the Cave Hard
loading Python…
Input
stones=[0, 1, 2, 4, 7]
Expected
True

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