All problems
0079MediumArrayDynamic ProgrammingGreedy

Stepping Stone Crossing

Tracked in this browser only
Write code

Trains the technique from

LeetCode 55Jump Game

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A ranger crosses a creek on a line of stepping stones, numbered from the near bank. Stone i carries a grip rating grips[i]: standing on that stone, the ranger can hop forward by any whole number of stones from 1 up to grips[i]. A rating of 0 means the stone gives no purchase at all, so no hop can start from it.

The ranger starts on stone 0 and wants to end up standing on the last stone. Return true if some sequence of hops gets there and false if none does. Hops never go backwards, and a hop that would carry the ranger past the last stone is not useful, so the last stone has to be landed on exactly.

A line of one stone means the ranger is already standing on the last stone, whatever that stone's rating is.

Examples

Example 1

Input
grips = [2, 3, 0, 1, 4]
Output
true

A single hop onto stone 1 buys a rating of 3, which is enough to land on the last stone; hopping two stones first would strand the ranger on the slick stone 2.

Example 2

Input
grips = [3, 1, 0, 0, 5]
Output
false

The best the ranger can do is stand on stone 3, and both stone 2 and stone 3 give no purchase, so the far side stays out of range.

Example 3

Input
grips = [0]
Output
true

The near bank stone is also the last stone, so the crossing is already done and the rating is irrelevant.

Constraints

  • 1 <= grips.length <= 10^4
  • 0 <= grips[i] <= 10^5

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def can_cross(grips: list[int]) -> bool:
Java
public boolean canCross(int[] grips)
September 7
Apply