-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminPathsum64.java
More file actions
28 lines (27 loc) · 1 KB
/
minPathsum64.java
File metadata and controls
28 lines (27 loc) · 1 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
public class minPathsum64 {
public static void main(String[] args){
int[][] grid = new int[][]{
{1,3,1},
{1,5,1},
{4,2,1}
};
System.out.println(minPathSum(grid));
}
public static int minPathSum(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
for(int i = grid.length - 1; i >= 0; i--) {
for (int j = grid[0].length - 1; j >=0; j--) {
if(i == grid.length - 1 && j != grid[0].length - 1) {
dp[i][j] = dp[i][j +1] + grid[i][j];
} else if(i != grid.length -1 && j == grid[0].length - 1) {
dp[i][j] = dp[i + 1][j] + grid[i][j];
} else if(i != grid.length - 1 && j != grid[0].length - 1) {
dp[i][j] = grid[i][j] + Math.min(dp[i+1][j], dp[i][j+1]);
} else {
dp[i][j] = grid[i][j];
}
}
}
return dp[0][0];
}
}