In a browser platform game, a sprite stands on platforms placed along a line. platforms lists their positions in increasing order, and the sprite starts on the first one, at position 0.
The sprite's first jump is exactly 1 unit. After a jump of k units, the next jump must be k - 1, k, or k + 1 units, always forward and always longer than 0. Every jump must land exactly on a platform.
Write canReachLast(platforms) that returns true if the sprite can land on the last platform, and false otherwise.
Input: platforms = [0,1,3,4,6,9,13,18] Output: true
Jumps of 1, 2, 3, 3, 4, 5 land on 1, 3, 6, 9, 13, 18.
Input: platforms = [0,1,2,3,4,8,9,11] Output: false
The sprite can land on 4 with a jump of at most 2, so its next jump is at most 3 and can't reach 8.
2 <= platforms.length <= 2000platforms[0] === 0, and positions are strictly increasing.platforms[i] <= 2^31 - 1Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #486 Sprite Platform Jumps. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.