Created
April 20, 2020 02:17
-
-
Save chrisynchen/736ef8a14f5f2b189531304c421fe827 to your computer and use it in GitHub Desktop.
minPathSum
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
public int minPathSum(int[][] grid) { | |
final int[][] dp = new int[grid.length][grid[0].length]; | |
dp[0][0] = grid[0][0]; | |
for(int i =0; i<grid.length;i++){ | |
for(int j =0; j<grid[0].length;j++){ | |
if (i == 0 && j == 0) continue; | |
if (i == 0) { | |
dp[0][j] = dp[0][j - 1] + grid[0][j]; | |
} else if (j == 0) { | |
dp[i][0] = dp[i - 1][0] + grid[i][0]; | |
} else { | |
dp[i][j] = Math.min(dp[i][j - 1] + grid[i][j], dp[i - 1][j] + grid[i][j]); | |
} | |
} | |
} | |
return dp[grid.length - 1][grid[0].length - 1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment