-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Path_Sum_64.java
More file actions
47 lines (34 loc) · 1.05 KB
/
Copy pathMinimum_Path_Sum_64.java
File metadata and controls
47 lines (34 loc) · 1.05 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
39
40
41
42
43
44
45
46
package Algorithms;
public class Minimum_Path_Sum_64 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] grid = {
{1, 2, 3} ,
{1, 2, 3}
} ;
Solution_Minimum_Path_Sum_64 s = new Solution_Minimum_Path_Sum_64() ;
int ans = s.minPathSum(grid) ;
System.out.println("ans: " + ans) ;
}
}
class Solution_Minimum_Path_Sum_64 {
public int minPathSum(int[][] grid) {
int[] row = new int[grid[0].length + 1] ;
//initial
row[0] = 99999 ;
row[1] = grid[0][0] ;
for(int i=2; i<row.length; i++) {
row[i] = row[i-1] + grid[0][i-1] ;
}
//work
for(int i=1; i<grid.length; i++) {
for(int j=1; j<row.length; j++) {
row[j] = row[j-1] < row[j] ? (row[j-1] + grid[i][j-1]) : (row[j] + grid[i][j-1]) ;
}
for(int k=0; k<row.length; k++) {
System.out.print(row[k] + " ") ;
}System.out.println("") ;
}
return row[row.length-1] ;
}
}