FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-basics/java-programs/DynamicProgramming/maxPath.java at master · Sethu1910/java-basics · GitHub
Sethu1910
/
java-basics
Public
forked from
learning-zone/java-basics
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-basics
/
java-programs
/
DynamicProgramming
/
maxPath.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (40 loc) · 1.08 KB
Breadcrumbs
java-basics
/
java-programs
/
DynamicProgramming
/
maxPath.java
Copy path
File metadata and controls
53 lines (40 loc) · 1.08 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
package
DynamicProgramming
;
import
java
.
util
.
Arrays
;
public
class
maxPath
{
public
static
int
maxPathSum
(
int
[][]
matrix
,
int
i
,
int
j
,
int
n
,
int
[][]
dp
) {
if
(
dp
[
i
][
j
] ==
0
) {
if
(
i
==
n
-
1
) {
return
matrix
[
i
][
j
];
}
int
num1
=
0
;
int
num2
=
0
;
int
num3
=
0
;
if
(
i
+
1
<
n
) {
num1
=
maxPathSum
(
matrix
,
i
+
1
,
j
,
n
,
dp
);
}
if
(
i
+
1
<
n
&&
j
-
1
> -
1
) {
num2
=
maxPathSum
(
matrix
,
i
+
1
,
j
-
1
,
n
,
dp
);
}
if
(
i
+
1
<
n
&&
j
+
1
<
n
) {
num3
=
maxPathSum
(
matrix
,
i
+
1
,
j
+
1
,
n
,
dp
);
}
dp
[
i
][
j
] =
matrix
[
i
][
j
] +
Math
.
max
(
num1
,
Math
.
max
(
num2
,
num3
));
}
return
dp
[
i
][
j
];
}
public
static
void
print
(
int
[][]
dp
) {
for
(
int
i
=
0
;
i
<
dp
.
length
;
i
++) {
System
.
out
.
println
(
Arrays
.
toString
(
dp
[
i
]));
}
}
public
static
void
main
(
String
[]
args
) {
// TODO Auto-generated method stub
int
[][]
matrix
= { {
2
} };
int
[][]
dp
=
new
int
[
matrix
.
length
][
matrix
.
length
];
int
ans
=
Integer
.
MIN_VALUE
;
for
(
int
i
=
0
;
i
<
matrix
.
length
;
i
++) {
ans
=
Math
.
max
(
ans
,
maxPathSum
(
matrix
,
0
,
i
,
matrix
.
length
,
dp
));
}
print
(
dp
);
}
}
Back
|
FazBrowse Home
|
New Git URL