@@ -25,7 +25,9 @@ to reach the last index.
2525## Solutions
2626
27271 . [ 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+
4347An optimized way to solve this problem is using a greedy approach that works in reverse. Instead of trying every
4448possible 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
70741 . 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
91951 . Set the last index of the array as the target index.
92962 . 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.
95993 . 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
99142The 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
104147The 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
0 commit comments