MEDIUM NC#123 Blind #26 Greedy

55. Jump Game

๐Ÿ“– Problem

You are given an integer array nums. You are initially positioned at nums[0]. Each element in the array represents your maximum jump length at that position. Return true if you can reach the last index of the array, or false otherwise.

๐Ÿง  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

Logical Thinking Concepts

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

๐Ÿ’ก Approach

  • โ†’ Track maximum reachable index
  • โ†’ If current index + nums[i] > max, can reach further
  • โ†’ If we can reach beyond array, return true
  • โ†’ Time: O(n), Space: O(1)

๐Ÿงญ Prerequisites

๐Ÿ› ๏ธ Hints & Pitfalls

Hints

  • โ€ขTrack maximum reachable index
  • โ€ขIf current index + nums[i] > max, can reach further
  • โ€ขIf we can reach beyond array, return true

Common Pitfalls

  • โ€ขTime: O(n), Space: O(1)

๐Ÿงช Test Cases

Hidden tests on submit: 1

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

๐Ÿ“ Code Editor

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit