-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMinimum Path Sum.java
More file actions
40 lines (34 loc) · 1.27 KB
/
Minimum Path Sum.java
File metadata and controls
40 lines (34 loc) · 1.27 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
/*
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
*/
public class Solution {
public int minPathSum(int[][] grid) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int rowLength = grid.length;
if (rowLength == 0) {
return 0;
}
int colLength = grid[0].length;
int[][] sum = new int[rowLength][colLength];
sum = grid.clone();
//initialize the first row and first column
for (int i = 1; i < colLength; i++) {
sum[0][i] += sum[0][i - 1];
}
for (int i = 1; i < rowLength; i++) {
sum[i][0] += sum[i - 1][0];
}
//build the rest using the base columns and rows
for (int i = 1; i < rowLength; i++) {
for (int j = 1; j < colLength; j++) {
int left = sum[i][j - 1];
int up = sum[i - 1][j];
int preSum = left < up ? left : up;
sum[i][j] += preSum;
}
}
return sum[rowLength - 1][colLength - 1];
}
}