FastPrepMinimum Jumps with Parity-Restricted Jump Lengths

Minimum Jumps with Parity-Restricted Jump Lengths

Google logoGoogle● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given a non-negative array nums. From index i, you may jump forward by any length from 1 through nums[i].

Jump numbers are one-based: the first jump must have odd length, the second jump even length, the third odd, and so on. Return the minimum jumps needed to reach the last index, or -1 if impossible.

An array of length one requires zero jumps.

Function

minimumParityJumps(nums: int[]) → int

Examples

Example 1

nums = [2,3,1,1,4]return = 3

Use jump lengths 1, 2, and 1 to reach indices 1, 3, and 4.

Example 2

nums = [3,2,1,0,4]return = -1

No parity-respecting sequence reaches the final index.

Example 3

nums = [0]return = 0

The start is already the destination.

Constraints

  • 1 <= nums.length <= 5000.
  • 0 <= nums[i] <= nums.length.

More Google problems

See Google hiring insights
public int minimumParityJumps(int[] nums) {
    // Write your code here.
}
nums[2,3,1,1,4]
expected3
Checking account…