Frog Jump

Hard

Topics
ArrayDynamic ProgrammingHash Table

A frog crosses a river by hopping on stones. The array nums gives the sorted positions of the stones (in units). The frog starts on the first stone (position 0) and its first jump must be exactly 1 unit. If the last jump was k units, the next jump must be k-1, k, or k+1 units and must be strictly positive. Return true if the frog can land on the last stone.

Example 1

Input:  stones = [0,1,3,5,6,8,12,17]
Output: true

Example 2

Input:  stones = [0,1,2,3,4,8,9,11]
Output: false

Constraints

  • 2 <= nums.length <= 2000
  • 0 <= nums[i] <= 2^31 - 1
  • nums[0] == 0 and nums is strictly increasing.
Run ⌘' · Submit ⌘⏎