Last active
April 18, 2020 20:32
-
-
Save IngeFrodo/2ba7d36f824d10520e2c20713a012321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinimumPathSum { | |
public int minPathSum(int[][] grid) { | |
if (grid == null || grid.length == 0 || grid[0].length == 0) { | |
return 0; | |
} | |
int m = grid.length; | |
int n = grid[0].length; | |
int dp[] = new int[n + 1]; | |
for (int j = 1; j <= n; j++) { | |
dp[j] = dp[j - 1] + grid[0][j - 1]; | |
} | |
for (int i = 1; i < m; i++) { | |
dp[0] += grid[i - 1][0]; | |
for (int j = 1; j < n; j++) { | |
dp[j] = Math.min(dp[j] + grid[i - 1][j], dp[j - 1] + grid[i][j - 1]); | |
} | |
dp[n] = dp[n - 1] + grid[i][n - 1]; | |
} | |
return dp[n]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment