-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumPathSum.java
More file actions
32 lines (26 loc) · 848 Bytes
/
Copy pathMinimumPathSum.java
File metadata and controls
32 lines (26 loc) · 848 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
//time:o(mn)
//space:o(mn)
class Solution {
public int minPathSum(int[][] grid) {
int r = grid.length, c = grid[0].length;
if(grid == null || r == 0) return 0;
//dp for path sum
int dp[][] = new int[r][c];
//starting point
dp[0][0] = grid[0][0];
//next down move plus start point, tmp path sum
for(int i = 1; i < r; i++){
dp[i][0]=dp[i-1][0]+grid[i][0];
}
//next right move plus start point, tmp path sum
for(int j = 1; j < c; j++){
dp[0][j]=dp[0][j-1] + grid[0][j];
}
for(int i = 1; i < r; i++){
for(int j =1; j < c; j++){
dp[i][j]=Math.min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}
}
return dp[r-1][c-1];
}
}