Fix a bug in the Minimum Path Sum solution

Check boundary
This commit is contained in:
Yutong Wang 2018-09-17 23:40:06 -07:00 committed by GitHub
parent e3c63b6637
commit 497812bd42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2505,9 +2505,9 @@ public int minPathSum(int[][] grid) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0) {
dp[j] = dp[j - 1];
if (j>0) dp[j] = dp[j - 1];
} else {
dp[j] = Math.min(dp[j - 1], dp[j]);
if (j>0) dp[j] = Math.min(dp[j - 1], dp[j]);
}
dp[j] += grid[i][j];
}