Skip to the code
+50 XP

#486 Sprite Platform Jumps

hard

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.

Example 1

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.

Example 2

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.

Constraints

  • 2 <= platforms.length <= 4000
  • platforms[0] === 0, and positions are strictly increasing.
  • platforms[i] <= 2^31 - 1
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).