Jumping across platforms
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- booleans
- loops
Problem
Level 4-2: a row of platforms over the lava, and the flag on the last. Each one has a number: from there you jump forward anywhere from 1 up to that many platforms, your choice; with a 0 you can't jump.
You get the list of numbers, whole numbers from 0 up, never empty. You start at position 0. Return true if you can reach the last platform and false if you can't; with just one, you are already at the flag.
With [2, 3, 1, 1, 4] you return true: from 0 you jump to 1, and from there to 4. With [3, 2, 1, 0, 4], false: every path ends on the 0 at position 3. Don't try out paths: walk the list keeping track of the farthest you can reach so far.
Examples
There is a way
[2, 3, 1, 1, 4] → true
Every path lands on the 0
[3, 2, 1, 0, 4] → false
Already at the flag
[0] → true
Stuck from the start
[0, 1] → false
A 0 at the goal doesn't matter
[2, 0, 0] → true
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def can_finish(jumps):
passJavaScript
function canFinish(jumps) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.