-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimalPathInMatrixBothDiagonal.java
More file actions
53 lines (46 loc) · 1.84 KB
/
Copy pathOptimalPathInMatrixBothDiagonal.java
File metadata and controls
53 lines (46 loc) · 1.84 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
package codes;
import java.util.Arrays;
/**
* Can travel only in down left or down right or down dir
* / | \
* / | \
*
*/
public class OptimalPathInMatrixBothDiagonal {
public static int maxPath(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int lengthY = matrix.length;
int lengthX = matrix[0].length;
int[][] pathMatrix = new int[lengthY][lengthX];
// Copy the first row as is from the matrix
for (int i = 1; i < lengthX; i++) {
pathMatrix[0][i] = matrix[0][i];
}
for (int i = 1; i < lengthY; i++) {
for (int j = 1; j < lengthX; j++) {
if(j == 0) { //for first colm
pathMatrix[i][j] = matrix[i][j]
+ Math.max(pathMatrix[i - 1][j], pathMatrix[i - 1][j + 1]);
} else if(j == lengthX-1) { //for last colm
pathMatrix[i][j] = matrix[i][j]
+ Math.max(pathMatrix[i - 1][j], pathMatrix[i - 1][j - 1]);
} else {
pathMatrix[i][j] = matrix[i][j]
+ Math.max(pathMatrix[i - 1][j],
Math.max(pathMatrix[i - 1][j - 1], pathMatrix[i - 1][j + 1]));
}
}
}
return Arrays.stream(pathMatrix[lengthY - 1]).max().orElse(0);
}
public static void main(String[] args) {
int[][] matrix = { { 3, 2, 8, 1, 1 },
{ 4, 8, 4, 6, 2 },
{ 7, 6, 8, 5, 3 },
{ 2, 9, 6, 8, 3 },
{ 7, 5, 9, 4, 8 } };
System.out.println(maxPath(matrix));
}
}