Skip to content

Instantly share code, notes, and snippets.

@IngeFrodo
Last active April 18, 2020 20:32
Show Gist options
  • Save IngeFrodo/2ba7d36f824d10520e2c20713a012321 to your computer and use it in GitHub Desktop.
Save IngeFrodo/2ba7d36f824d10520e2c20713a012321 to your computer and use it in GitHub Desktop.
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