View on NeetCode
View on LeetCode

Problem

You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

Constraints:

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^5

Solution

Approach 1: Greedy (Forward)

The key insight is to track the furthest position we can reach. If at any point we can’t progress further, we can’t reach the end.

Implementation

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        max_reach = 0

        for i in range(len(nums)):
            # If current position is beyond our reach, can't proceed
            if i > max_reach:
                return False

            # Update furthest position we can reach
            max_reach = max(max_reach, i + nums[i])

            # Early exit if we can reach the end
            if max_reach >= len(nums) - 1:
                return True

        return True

Approach 2: Greedy (Backward)

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        # Start from the end, track the leftmost position that can reach end
        goal = len(nums) - 1

        for i in range(len(nums) - 2, -1, -1):
            # If we can reach goal from position i
            if i + nums[i] >= goal:
                goal = i

        return goal == 0

Approach 3: Dynamic Programming

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n = len(nums)
        dp = [False] * n
        dp[0] = True

        for i in range(n):
            if not dp[i]:
                continue

            # From position i, mark all reachable positions
            for j in range(1, nums[i] + 1):
                if i + j < n:
                    dp[i + j] = True

        return dp[n - 1]

Complexity Analysis

Greedy Approaches:

  • Time Complexity: O(n), single pass through array.
  • Space Complexity: O(1), constant extra space.

DP Approach:

  • Time Complexity: O(n * k), where k is average jump length.
  • Space Complexity: O(n) for dp array.

Key Insights

  1. Greedy Optimality: Tracking furthest reachable position is sufficient - we don’t need to track all possible paths.

  2. Forward Scan: At each position, update the maximum index we can reach from that position.

  3. Backward Scan: Work backwards from the end, finding the leftmost position that can reach our current goal.

  4. Early Termination: If at any point we can reach the last index, return true immediately.

  5. Unreachable Detection: If current position is beyond our maximum reach, we can’t proceed further.

  6. Greedy Choice Property: Always extending our reach as far as possible is optimal.