forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumpathsum.java
More file actions
executable file
·28 lines (27 loc) · 873 Bytes
/
minimumpathsum.java
File metadata and controls
executable file
·28 lines (27 loc) · 873 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
public class Solution {
public int minPathSum(int[][] grid) {
// Start typing your Java solution below
// DO NOT write main() function
if(grid.length==0){
return 0;
}
if(grid[0].length==0){
return 0;
}
int [][]m = new int[grid.length][grid[0].length];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(i==0 && j==0){
m[i][j] = grid[i][j];
}else if(i==0){
m[i][j] = grid[i][j] + m[i][j-1];
}else if(j==0){
m[i][j] = grid[i][j]+m[i-1][j];
}else{
m[i][j] = Math.min(m[i][j-1],m[i-1][j])+grid[i][j];
}
}
}
return m[grid.length-1][grid[0].length-1];
}
}