-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPathSum.java
More file actions
38 lines (31 loc) · 1.09 KB
/
MaxPathSum.java
File metadata and controls
38 lines (31 loc) · 1.09 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
package DynamicProgramming;
import java.util.HashMap;
import java.util.List;
public class MaxPathSum {
public static void main(String[] args) {
List<List<Integer>> boardList = List.of(
List.of(5, 3, 2),
List.of(1, 4, 6)
);
HashMap<List<Integer>, Integer> memo = new HashMap<>();
int maxSum = maxPathListInt(boardList, 0, 0, memo);
System.out.println("Max Path Sum: " + maxSum);
}
static int maxPathListInt(List<List<Integer>> board,int row,int col,HashMap<List<Integer>,Integer> memo) {
if(row >= board.size() || col >= board.get(0).size()){
return 0;
};
if(row == 1 && col == 2) {
return board.get(row).get(col);
}
List<Integer> pos = List.of(row,col);
if(memo.containsKey(pos)) {
return memo.get(pos);
}
int left = maxPathListInt(board,row,col+1,memo);
int right = maxPathListInt(board,row+1,col,memo);
int res = board.get(row).get(col) + Math.max(left, right);
if(res > 0) memo.put(pos,res);
return res;
}
}