-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame2.java
More file actions
355 lines (263 loc) · 8.46 KB
/
JumpGame2.java
File metadata and controls
355 lines (263 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package Algorithms.GreedyAlgorithms;
import java.util.Arrays;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 26 Feb 2025
* @link 45. Jump Game II <a href="https://leetcode.com/problems/jump-game-ii/">Leetcode link</a>
* @topics Array, Greedy, Dynamic Programming
* @companies Amazon, Zoho, Meta, Microsoft, TikTok, Nutanix, Google, Bloomberg, ServiceNow, HashedIn, Adobe, Apple, Oracle, Uber, DoorDash, DE, PhonePe, Expedia, tcs, Flipkart
*/
public class JumpGame2 {
public static void main(String[] args) {
int[] nums = {2,3,1,1,4};
System.out.printf("jump using Greedy MaxJump => %d%n", jumpUsingGreedyMaxJump(nums));
System.out.printf("jumpUsing using Greedy Bfs => %d%n", jumpUsingGreedyBfs(nums));
System.out.printf("jumpUsingDfs => %d%n", jumpUsingDfs(nums));
}
/**
* Using Greedy
i = index
e = endI or currEnd
j = maxJump or currFar
INITIALLY ---
[2, 3, 1, 1, 4] maxJump=0, jump=0
i
je
[2, 3, 1, 1, 4] maxJump=2, jump=1
i
je
[2, 3, 1, 1, 4] maxJump=4, jump=1
i
e j
[2, 3, 1, 1, 4] maxJump=4, jump=2
i
ej
[2, 3, 1, 1, 4] maxJump=4, jump=2
i
ej
*/
public static int jumpUsingGreedyMaxJump(int[] nums) {
int jumps = 0, maxJump = 0, endI = 0;
for (int i = 0; i < nums.length - 1; i++) {
maxJump = Math.max(maxJump, i + nums[i]);
if (i == endI) {
jumps++;
endI = maxJump;
}
}
return jumps;
}
/**
* Just imagine as sections or levels --> calculate after how many levels we reach the end
* <pre>
0 1 2 3 4
[ 2, 3, 1, 1, 4 ]
|_| |__| |___|
1 2 3
lr l r l r
Here, 0th index 2 is 1st level, and we can jump to 1,2 indices, then consider this 1,2 indices as 2nd level
Now check the 3rd level we can jump from from 2nd level, if the 3rd level contains n-1 then we can return the jumps
INITIALLY ---
[2, 3, 1, 1, 4] newR=0
i
|_| level 1
lr
[2, 3, 1, 1, 4] newR=2
i
|__| level 2
l r
[2, 3, 1, 1, 4] newR=4
i
|___| level 3 ---> r>=n-1 stop
l r
* </pre>
*/
public static int jumpUsingGreedyBfs(int[] nums) {
int l=0, r=0, jumps=0, n=nums.length;
while(r<n-1) { // ----> stop when we came across the last level
/*
calculate next level boundaries --> next l & r
--> we already know that nextL=currR+1.
So basically we calculate the nextR
*/
int farthestJump=0; // nextR
for(int i=l; i<=r; i++) {
farthestJump = Math.max(farthestJump, i+nums[i]);
}
l=r+1; // nextL=currR+1
r=farthestJump; // nextR
jumps++;
}
return jumps;
}
/**
* Using Greedy --> similar to {@link #jumpUsingGreedyBfs}
0 1 2 3 4
[ 2, 3, 1, 1, 4 ]
|_| |__| |___|
1 2 3
lr l r l r
*/
public static int jumpUsingGreedyBfs2(int[] nums) {
if (nums.length == 1) {
return 0;
}
int l = 0;
int r = 0;
int jumps = 1;
for (int i = 0; i < nums.length; i++) { // or while(r < n-1) {}
int newR = 0; // or farthest
for (int j = l; j <= r; j++) {
newR = Math.max(newR, nums[j] + j);
}
if (r >= nums.length - 1) {
break;
}
jumps++;
l = r + 1;
r = newR;
}
return jumps;
}
/**
[-1,-1,-1,-1, 0]
[2, 3, 0, 1, 4]
i
[-1,-1,-1, 1, 0]
[2, 3, 0, 1, 4]
i
[-1,-1, n, 1, 0]
[2, 3, 0, 1, 4]
i
[-1, 1, n, 1, 0]
[2, 3, 0, 1, 4]
i
[2, 1, n, 1, 0]
[2, 3, 0, 1, 4]
i
[-1,-1,-1, 0]
[1, 1, 1, 1]
i
[-1,-1, 1, 0]
[1, 1, 1, 1]
i
[-1, 2, 1, 0]
[1, 1, 1, 1]
i
[3, 2, 1, 0]
[1, 1, 1, 1]
i
*/
public static int jumpUsingDfs(int[] nums) {
int n = nums.length;
if(n==1) return 0;
int[] dp = new int[n];
Arrays.fill(dp, -1); // to cover the edge case in dfs recursion or use Integer[] dp
dp[n-1]=0;
for(int i=n-2; i>=0; i--) {
dfs(nums, i, dp);
}
return dp[0];
}
private static int dfs(int[] nums, int i, int[] dp) {
int n = nums.length;
if(dp[i] != -1) return dp[i]; // Arrays.fill(dp, -1); edge case or use Integer[] dp instead of int[]
else if(nums[i] >= n-1-i) return dp[i] = 1;
else if (nums[i]==0) return dp[i] = n;
int minJump = n;
for(int j=i+1; j-i <= nums[i]; j++) { // && j<n is not needed as we have "nums[i] >= n-1-i" condition check at top
minJump = Math.min(minJump, dfs(nums, j, dp)+1 );
}
return dp[i] = minJump;
}
/**
* Top-Down DP with no memo, just recursion i.e., backtracking
* TLE
* */
public int jumpBacktracking(int[] nums) {
if(nums.length==1) return 0;
return dfs(nums, 0, 1);
}
private int dfs(int[] nums, int i, int currJump) {
int n = nums.length;
if (nums[i]+i+1 >= n) return currJump; // BASE CASE
else if (nums[i]==0) return Integer.MAX_VALUE;
// we need to jump again i.e +1
int minJump=Integer.MAX_VALUE;
for(int j=i+1; j<n && j-i <= nums[i]; j++) {
minJump = Math.min(minJump, dfs(nums, j, currJump+1));
}
return minJump;
}
public int jumpTopDownMemoDp(int[] nums) {
int n = nums.length;
if (n == 1) return 0;
int[] dp = new int[n];
Arrays.fill(dp, -1);
dp[n - 1] = 0;
return dfs1(nums, 0, dp);
}
private int dfs1(int[] nums, int i, int[] dp) {
if (dp[i] != -1) return dp[i];
int n = nums.length;
int minJump = Integer.MAX_VALUE;
for (int j = i + 1; j <= Math.min(i + nums[i], n - 1); j++) {
int jumps = dfs1(nums, j, dp);
if (jumps != Integer.MAX_VALUE) {
minJump = Math.min(minJump, 1 + jumps);
}
}
return dp[i] = minJump;
}
public int jumpTopDownMemoDp2(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
Arrays.fill(dp, -1);
return dfs2(nums, 0, dp);
}
private int dfs2(int[] nums, int i, int[] dp) {
int n = nums.length;
if (i >= n - 1) return 0; // already at or beyond the end
if (dp[i] != -1) return dp[i];
if (nums[i] == 0) return dp[i] = n; // can't move
int minJump = n;
for (int j = i + 1; j <= Math.min(i + nums[i], n - 1); j++) {
int jumps = dfs2(nums, j, dp);
if (jumps != n) {
minJump = Math.min(minJump, 1 + jumps);
}
}
return dp[i] = minJump;
}
// NOT WORKING
public int canJumpMyApproachOld(int[] nums) {
int min = 0;
if (nums.length==1) return min;
for (int i=nums.length-1; i>=0; i--){
if ((nums[i] + i) >= (nums.length-1)) {
nums[i] = -1;
min = 1;
continue;
}
if (isJumpFound(nums, i)) {
nums[i] = -1; // "flag" that index as --> can jump
min++;
}
System.out.println(Arrays.toString(nums));
System.out.println(min);
}
if (nums[0]==-1) {
return min;
}
return -1;
}
private static boolean isJumpFound(int[] nums, int start) {
int end = (start+nums[start])>=nums.length? nums.length-1: start+nums[start];
int i = end;
for (;(end-i)<nums[start] && i>-1; i--) {
if (nums[i]==-1) return true;
}
// return end-1 ---> and subtract the same from min??
return false;
}
}