Skip to content

Commit d374048

Browse files
committed
docs(algorithms, greedy): jump game
1 parent b71f628 commit d374048

9 files changed

Lines changed: 52 additions & 10 deletions

algorithms/greedy/jump_game/README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ to reach the last index.
2525
## Solutions
2626

2727
1. [Naive Approach](#naive-approach)
28-
1. [Optimized Approach using Greedy Pattern](#optimized-approach-using-greedy-pattern)
28+
2. [Optimized Approach using Greedy Pattern](#optimized-approach-using-greedy-pattern)
29+
- [Variation 1](#variation-1)
30+
- [Variation 2](#variation-2)
2931

3032
### Naive Approach
3133

@@ -40,6 +42,8 @@ for large inputs.
4042

4143
### Optimized Approach using Greedy Pattern
4244

45+
#### Variation 1
46+
4347
An optimized way to solve this problem is using a greedy approach that works in reverse. Instead of trying every
4448
possible forward jump, we flip the logic: start from the end and ask, “Can we reach this position from any earlier index?”
4549

@@ -65,7 +69,7 @@ target remains beyond index 0, then no path exists from the start to the end, an
6569
![Solution 10](./images/solutions/jump_game_1_solution_10.png)
6670
![Solution 11](./images/solutions/jump_game_1_solution_11.png)
6771

68-
#### Algorithm
72+
###### Algorithm
6973

7074
1. We begin by setting the last index of the array as our initial target using the variable `target = len(nums) - 1`. This
7175
target represents the position we are trying to reach, starting from the end and working backward. By initializing the
@@ -86,14 +90,53 @@ target remains beyond index 0, then no path exists from the start to the end, an
8690
- Or reach the start without ever being able to update the target to 0, which means there is no valid path. In this
8791
case, we return FALSE.
8892

89-
#### Solution Summary
93+
###### Solution Summary
9094

9195
1. Set the last index of the array as the target index.
9296
2. Traverse the array backward and verify if we can reach the target index from any of the previous indexes.
9397
- If we can reach it, we update the target index with the index that allows us to jump to the target index.
9498
- We repeat this process until we’ve traversed the entire array.
9599
3. Return TRUE if, through this process, we can reach the first index of the array. Otherwise, return FALSE.
96100

101+
#### Variation 2
102+
103+
Think of each index as a platform. The value at each platform tells you the maximum distance you can jump from there.
104+
You need to figure out: can you reach the goal?
105+
106+
![Solution 2.1](./images/solutions/jump_game_1_solution_2.1.png)
107+
108+
The question isn't about how to get there - just whether it's possible.
109+
110+
We don't need to track every possible path. We just need to track the farthest position we can reach so far.
111+
112+
![Solution 2.2](./images/solutions/jump_game_1_solution_2.2.png)
113+
114+
As we iterate through the array:
115+
- At each position i, update max_reach = max(max_reach, i + nums[i])
116+
- If i > max_reach, we're stuck - return false
117+
- If we finish the loop, return true
118+
119+
- Let's trace through nums = [2, 3, 1, 1, 4]:
120+
121+
![Solution 2.3](./images/solutions/jump_game_1_solution_2.3.png)
122+
123+
i=0: Can we reach index 0? Yes (we start here). From here, we can reach up to index 2. max_reach = 2.
124+
125+
![Solution 2.4](./images/solutions/jump_game_1_solution_2.4.png)
126+
127+
i=1: Can we reach index 1? Yes (1 ≤ 2). From here, we can reach up to index 4! max_reach = 4.
128+
129+
![Solution 2.5](./images/solutions/jump_game_1_solution_2.5.png)
130+
131+
i=2: Can we reach index 2? Yes (2 ≤ 4). max_reach stays 4.
132+
We continue... max_reach = 4 >= n-1 = 4. Answer: true!
133+
134+
What if we can't reach the end? Consider nums = [3, 2, 1, 0, 4]:
135+
136+
![Solution 2.6](./images/solutions/jump_game_1_solution_2.6.png)
137+
138+
At index 3, nums[3] = 0 means we can't jump anywhere. Since max_reach = 3 and we need to reach index 4, we're stuck.
139+
97140
#### Time Complexity
98141

99142
The time complexity of the above solution is O(n), since we traverse the array only once, where n is the number of
@@ -103,7 +146,6 @@ elements in the array.
103146

104147
The space complexity of the above solution is O(1), because we do not use any extra space.
105148

106-
107149
---
108150

109151
# Jump Game II

algorithms/greedy/jump_game/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ def can_jump(nums: List[int]) -> bool:
1010
bool: True if can jump to the last index, False otherwise
1111
"""
1212

13-
current_position = nums[0]
13+
max_reach = 0
1414

15-
for idx in range(1, len(nums)):
16-
if current_position == 0:
15+
for i in range(len(nums)):
16+
if i > max_reach:
1717
return False
1818

19-
current_position -= 1
20-
21-
current_position = max(current_position, nums[idx])
19+
max_reach = max(max_reach, i + nums[i])
2220

2321
return True
2422

36.2 KB
Loading
39.8 KB
Loading
32.4 KB
Loading
31.4 KB
Loading
32.9 KB
Loading
35.5 KB
Loading

algorithms/greedy/jump_game/test_jump_game.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
([2, 3, 1, 1, 9], True),
1414
([4, 0, 0, 0, 4], True),
1515
([1], True),
16+
([2, 0, 0], True),
17+
([5, 9, 3, 2, 1, 0, 2, 3, 3, 1, 0, 0], True),
1618
]
1719

1820

0 commit comments

Comments
 (0)