Jump Game
Real-world version: Stepping Stone CrossingThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def can_cross(grips: list[int]) -> bool:public boolean canCross(int[] grips)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Best Time to Buy and Sell Stock IIReal-world version: Collectible Resale RunMedium
- Jump Game IIReal-world version: Fewest Drone FlightsMedium
- Non-overlapping IntervalsReal-world version: Drop the Clashing Kiln RunsMedium
- Best Time to Buy and Sell Stock with Transaction FeeReal-world version: Handling Fee ResaleMedium