FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/JumpGame.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
JumpGame.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
97 lines (74 loc) · 2.49 KB
Breadcrumbs
coding-Interview
/
JumpGame.java
Copy path
File metadata and controls
97 lines (74 loc) · 2.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
leetcode
;
import
java
.
util
.
HashMap
;
public
class
JumpGame
{
//https://leetcode.com/problems/jump-game/
/**
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
**/
class
Solution
{
public
boolean
canJump
(
int
[]
nums
) {
int
reach
=
0
,
n
=
nums
.
length
;
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
reach
=
Math
.
max
(
reach
,
i
+
nums
[
i
]);
if
(
reach
>=
n
-
1
)
return
true
;
if
(!(
reach
>
i
))
////means cannot go ahead than current reach < i or reach == i
return
false
;
}
return
true
;
}
}
// Recursion with Memo
class
SolutionTLE
{
boolean
canReach
;
//just need one path
public
boolean
canJump
(
int
[]
nums
) {
HashMap
<
Integer
,
Boolean
>
memo
=
new
HashMap
<>();
// to store if at index if can be reached.
int
n
=
nums
.
length
;
return
backtrack
(
0
,
n
-
1
,
nums
,
memo
);
}
public
boolean
backtrack
(
int
start
,
int
target
,
int
[]
nums
,
HashMap
<
Integer
,
Boolean
>
memo
){
if
(
memo
.
containsKey
(
start
))
return
memo
.
get
(
start
);
if
(
canReach
)
return
false
;
//terminate if any one solution is found
if
(
start
==
target
){
memo
.
put
(
start
,
true
);
canReach
=
true
;
return
true
;
}
int
jump
=
nums
[
start
];
boolean
found
=
false
;
for
(
int
i
=
1
;
i
<=
jump
;
i
++){
if
(
start
+
i
<=
target
)
found
=
found
||
backtrack
(
start
+
i
,
target
,
nums
,
memo
);
}
memo
.
put
(
start
,
found
);
return
found
;
}
}
/**
* Simple Recursion solution to add memo later.
*/
class
SolutionSimpleRec
{
boolean
canReach
;
//just need one path
public
boolean
canJump
(
int
[]
nums
) {
canReach
=
false
;
int
n
=
nums
.
length
;
backtrack
(
0
,
n
-
1
,
nums
);
return
canReach
;
}
public
void
backtrack
(
int
start
,
int
target
,
int
[]
nums
){
if
(
canReach
)
return
;
//terminate if any one solution is found
if
(
start
==
target
){
canReach
=
true
;
return
;
}
int
jump
=
nums
[
start
];
for
(
int
i
=
1
;
i
<=
jump
;
i
++){
if
(
start
+
i
<=
target
)
backtrack
(
start
+
i
,
target
,
nums
);
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL