Skip to content

Instantly share code, notes, and snippets.

@autekroy
Created October 28, 2014 17:42
Show Gist options
  • Save autekroy/71cab846dca780089c02 to your computer and use it in GitHub Desktop.
Save autekroy/71cab846dca780089c02 to your computer and use it in GitHub Desktop.
LeetCode OJ - Minimum Path Sum
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int column = grid[0].size();
int row = grid.size();
int dp[column];
memset(dp, 0, sizeof(dp));
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
if(i == 0 && j >0) dp[j] = grid[i][j] + dp[j - 1];
else if(j == 0) dp[j] = dp[j] + grid[i][j];
else dp[j] = min(dp[j-1], dp[j]) + grid[i][j];
}
}
return dp[column - 1];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment