-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJump Game.java
More file actions
82 lines (68 loc) · 2.28 KB
/
Jump Game.java
File metadata and controls
82 lines (68 loc) · 2.28 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
/*
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
Example
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
This can be done using DP. However, greedy algorithm is fast in this particular problem. Consider both solutions.
DP
Thinking Process:
We have array A, that stores the # of steps for each index.
State: f[i] means if previous steps can reach to i. True/False
Function: f[i] = f[j] && (j + A[j] >= i)
Init: f[0] = true
Answer: f[n-1], if n is the length of A
*/
public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
**/
//DP
public boolean canJump(int[] A) {
if (A == null || A.length == 0) {
return false;
}
//By default, boolean[] can is all false
boolean[] can = new boolean[A.length];
can[0] = true;
for (int i = 1; i < A.length; i++) {
for (int j = 0; j < i; j++) {
if (A[j] && (j + A[j] >= i)) {
can[i] = true;
break;
}
}
}
return can[A.length - 1];
}
}
/*
Greedy. Ideas from Yu’s Garden
At each index, check how far we can jump, store this forest-can-jump position in variable ‘farest’. Take max of current farest and (index + A[index]), store is in farest
At each index, compare if ‘farest’ is greater than the end of array, if so, found solution, return true.
At each index, also check if ‘farest == current index’, that means the farest we can move is to current index and we cannot move forward. Then return false.
*/
public class Solution {
/**
* @param A: A list of integers
* @return: The boolean answer
**/
public boolean canJump(int[] A) {
if (A == null || A.length == 0) {
return false;
}
int farest = 0;
for (int i = 0; i < A.length; i++) {
farest = Math.max(farest, i + A[i]);
if (farest > A.length - 1) {
return true;
}
if (farest == i) {
return false;
}
}
return true;
}
}