🧠
Brute Force (Pure Recursion)
💡 This approach exists to build intuition by exploring all possible jump paths recursively. It helps understand the problem's exponential nature and why optimization is necessary.
Intuition
From each position, try every possible jump length and recursively find the minimum jumps to the end. The minimum among all these paths is the answer.
Algorithm
- Start at index 0.
- If at the last index, return 0 jumps needed.
- For each jump length from 1 to nums[current], recursively compute jumps needed from the new position.
- Return 1 plus the minimum jumps from all recursive calls.
💡 The recursion tree grows exponentially because from each position, multiple jumps are possible, making it hard to track without optimization.
def jump(nums):
def dfs(pos):
if pos >= len(nums) - 1:
return 0
min_jumps = float('inf')
furthest_jump = min(pos + nums[pos], len(nums) - 1)
for next_pos in range(pos + 1, furthest_jump + 1):
jumps = dfs(next_pos)
if jumps != float('inf'):
min_jumps = min(min_jumps, jumps + 1)
return min_jumps
return dfs(0)
# Example usage
if __name__ == '__main__':
print(jump([2,3,1,1,4])) # Output: 2
Line Notes
def dfs(pos):Defines a recursive helper to compute min jumps from position pos
if pos >= len(nums) - 1:Base case: if at or beyond last index, no more jumps needed
for next_pos in range(pos + 1, furthest_jump + 1):Try all possible jumps from current position
min_jumps = min(min_jumps, jumps + 1)Update minimum jumps including current jump
public class Solution {
public int jump(int[] nums) {
return dfs(nums, 0);
}
private int dfs(int[] nums, int pos) {
if (pos >= nums.length - 1) return 0;
int minJumps = Integer.MAX_VALUE;
int furthestJump = Math.min(pos + nums[pos], nums.length - 1);
for (int nextPos = pos + 1; nextPos <= furthestJump; nextPos++) {
int jumps = dfs(nums, nextPos);
if (jumps != Integer.MAX_VALUE) {
minJumps = Math.min(minJumps, jumps + 1);
}
}
return minJumps;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.jump(new int[]{2,3,1,1,4})); // Output: 2
}
}
Line Notes
public int jump(int[] nums)Entry point calling recursive dfs from index 0
if (pos >= nums.length - 1) return 0;Base case: no jumps needed if at or beyond last index
for (int nextPos = pos + 1; nextPos <= furthestJump; nextPos++)Try all jumps from current position
minJumps = Math.min(minJumps, jumps + 1);Update minimum jumps including current jump
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int dfs(const vector<int>& nums, int pos) {
if (pos >= (int)nums.size() - 1) return 0;
int minJumps = INT_MAX;
int furthestJump = min(pos + nums[pos], (int)nums.size() - 1);
for (int nextPos = pos + 1; nextPos <= furthestJump; ++nextPos) {
int jumps = dfs(nums, nextPos);
if (jumps != INT_MAX) {
minJumps = min(minJumps, jumps + 1);
}
}
return minJumps;
}
int jump(vector<int>& nums) {
return dfs(nums, 0);
}
int main() {
vector<int> nums = {2,3,1,1,4};
cout << jump(nums) << endl; // Output: 2
return 0;
}
Line Notes
int dfs(const vector<int>& nums, int pos)Recursive helper to find min jumps from pos
if (pos >= (int)nums.size() - 1) return 0;Base case: no jumps needed if at or beyond last index
for (int nextPos = pos + 1; nextPos <= furthestJump; ++nextPos)Try all possible jumps from current position
minJumps = min(minJumps, jumps + 1);Update minimum jumps including current jump
function jump(nums) {
function dfs(pos) {
if (pos >= nums.length - 1) return 0;
let minJumps = Infinity;
let furthestJump = Math.min(pos + nums[pos], nums.length - 1);
for (let nextPos = pos + 1; nextPos <= furthestJump; nextPos++) {
let jumps = dfs(nextPos);
if (jumps !== Infinity) {
minJumps = Math.min(minJumps, jumps + 1);
}
}
return minJumps;
}
return dfs(0);
}
// Example usage
console.log(jump([2,3,1,1,4])); // Output: 2
Line Notes
function dfs(pos) {Recursive helper to compute min jumps from pos
if (pos >= nums.length - 1) return 0;Base case: no jumps needed if at or beyond last index
for (let nextPos = pos + 1; nextPos <= furthestJump; nextPos++) {Try all jumps from current position
minJumps = Math.min(minJumps, jumps + 1);Update minimum jumps including current jump
TimeO(2^n)
SpaceO(n) due to recursion stack
At each index, we try all possible jumps leading to an exponential number of recursive calls.
💡 For n=20, this means over a million calls, which is impractical.
Interview Verdict: TLE
This approach is too slow for large inputs but is useful to understand the problem's nature and motivate better solutions.