Created
October 28, 2014 17:42
-
-
Save autekroy/71cab846dca780089c02 to your computer and use it in GitHub Desktop.
LeetCode OJ - Minimum Path Sum
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 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