-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMinimumPathSum.java
More file actions
43 lines (37 loc) · 897 Bytes
/
MinimumPathSum.java
File metadata and controls
43 lines (37 loc) · 897 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
34
35
36
37
38
39
40
41
42
43
public class Solution {
public int minPathSum(int[][] grid) {
// Start typing your Java solution below
// DO NOT write main() function
int rows = grid.length;
int cols = grid[0].length;
int sum = 0;
if(rows == 0) return 0;
if(rows == 1){
for(int i=0; i<cols; i++)
sum += grid[0][i];
return sum;
}
if(cols == 1){
for(int i=0; i<rows;i++)
sum += grid[i][0];
return sum;
}
int[][] path = new int[rows][cols];
path[0][0] = 0;
for(int i=1; i<cols; i++){
sum += grid[0][i-1];
path[0][i] = sum;
}
sum = 0;
for(int i=1; i<rows; i++){
sum += grid[i-1][0];
path[i][0] = sum;
}
for(int i=1; i<rows; i++)
for(int j=1; j<cols; j++){
int temp = Math.min(grid[i-1][j]+path[i-1][j], grid[i][j-1]+path[i][j-1]);
path[i][j] = temp;
}
return path[rows-1][cols-1]+grid[rows-1][cols-1];
}
}