Leetcode #2770: Maximum Number of Jumps to Reach the Last Index
In this guide, we solve Leetcode #2770 Maximum Number of Jumps to Reach the Last Index in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0.
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Array, Dynamic Programming
Intuition
The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.
A carefully chosen DP state captures exactly what we need to build the final answer.
Approach
Define the DP state and recurrence, then compute states in the correct order.
Optionally compress space once the recurrence is clear.
Steps:
- Choose a DP state definition.
- Write the recurrence and base cases.
- Compute states in the correct order.
Example
Input: nums = [1,3,6,4,1,2], target = 2
Output: 3
Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
- Jump from index 0 to index 1.
- Jump from index 1 to index 3.
- Jump from index 3 to index 5.
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3.
Python Solution
class Solution:
def maximumJumps(self, nums: List[int], target: int) -> int:
def dfs(i: int) -> int:
if i == n - 1:
return 0
ans = -inf
for j in range(i + 1, n):
if abs(nums[i] - nums[j]) <= target:
ans = max(ans, 1 + dfs(j))
return ans
n = len(nums)
ans = dfs(0)
return -1 if ans < 0 else ans
Complexity
The time complexity is , and the space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.