欢迎来到Cefler的博客
博客主页:那个传说中的man的主页
个人专栏:题目解析
推荐文章:题目大解析(3)
mycode:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m+1,vector<int>(n+1 ));
dp[0][1] = 1;
for(int i = 1;i<=m;i++)
{
for(int j = 1;j<=n;j++)
{
dp[i][j] = dp[i-1][j]+dp[i][j-1];
}
}
return dp[m][n];
}
};
原题链接:不同路径II
mycode:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size(),n = obstacleGrid[0].size();
vector<vector<int>> dp(m+1,vector<int>(n+1 ));
dp[0][1] = 1;
for(int i = 1;i<=m;i++)
{
for(int j = 1;j<=n;j++)
{
dp[i][j] = dp[i-1][j]+dp[i][j-1];
if(obstacleGrid[i-1][j-1]==1)
dp[i][j] = 0;
}
}
return dp[m][n];
}
};
mycode:
class Solution {
public:
int jewelleryValue(vector<vector<int>>& frame) {
int m = frame.size(),n = frame[0].size();
vector<vector<int>> dp(m+1,vector<int>(n+1));
for(int i = 1;i<=m;i++)
{
for(int j = 1;j<=n;j++)
{
dp[i][j] = max(dp[i-1][j]+frame[i-1][j-1],dp[i][j-1]+frame[i-1][j-1]);
}
}
return dp[m][n];
}
};