-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution64.java
More file actions
33 lines (29 loc) · 842 Bytes
/
Copy pathSolution64.java
File metadata and controls
33 lines (29 loc) · 842 Bytes
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
package leecode;
public class Solution64 {
public static void main(String[] args) {
Solution64 s = new Solution64();
System.out.println(s.minPathSum(new int[][]{{0,1},{1,0}}));
System.out.println(s.minPathSum(new int[][]{{1,3,1},{1,5,1},{4,2,1}}));
/*
1 3 1
1 5 1
4 2 1
*/
}
public int minPathSum(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
for (int i = 1; i < m; i++) {
grid[i][0] += grid[i-1][0];
}
for (int i = 1; i < n; i++) {
grid[0][i] += grid[0][i-1];
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]);
}
}
return grid[m-1][n-1];
}
}