MEDIUM NC#124 Greedy / BFS

45. Jump Game II

📖 Problem

You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0]. Each element nums[i] represents the maximum jump length at that position. Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • Array methods (`push`, `pop`, `shift`, `slice`)
  • Object/Map/Set usage patterns
  • Function parameter and return typing
  • Level-order traversal
  • Queue discipline
  • Shortest-step interpretation

Logical Thinking Concepts

  • Define invariants before coding
  • Check edge cases first (`[]`, single element, duplicates)
  • Estimate time/space before implementation
  • Apply BFS reasoning pattern
  • Apply Greedy reasoning pattern

💡 Approach

  • Track the farthest reachable position in current jump
  • When current index equals the end of current jump's range, increment jump count
  • Time: O(n), Space: O(1)

🛠️ Hints & Pitfalls

Hints

  • Track the farthest reachable position in current jump
  • When current index equals the end of current jump's range, increment jump count
  • Time: O(n), Space: O(1)

🧪 Test Cases

Hidden tests on submit: 1

Test Case 1
Not run
Input:
jump([2,3,1,1,4]);
Expected:
2
Test Case 2
Not run
Input:
jump([2,3,0,1,4]);
Expected:
2
Test Case 3
Not run
Input:
jump([0]);
Expected:
0

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit