i升序:
class Solution {
public:
int dp[1010];
int minimumTotal(vector<vector<int>>& triangle) {
int i,j;
dp[0]=triangle[0][0];
int val;
for(i=1;i<triangle.size();i++)
for(j=0;j<triangle[i].size();j++)
{
if(j-1<0)
{
val=dp[j];
dp[j]=triangle[i][j]+dp[j];
}
else
{
if(j>=triangle[i-1].size())
{
dp[j]=triangle[i][j]+val;
val=dp[j];
}
else
{
int tmp=triangle[i][j]+min(val,dp[j]);
val=dp[j];
dp[j]=tmp;
}
}
}
int ans=INT_MAX;
for(j=0;j<triangle[i-1].size();j++)
ans=min(ans,dp[j]);
return ans;
}
};
i降序:
class Solution {
public:
int dp[1010];
int minimumTotal(vector<vector<int>>& triangle) {
int i,j;
if(triangle.size()==1)
return triangle[0][0];
for(j=0;j<triangle[triangle.size()-1].size();j++)dp[j]=triangle[triangle.size()-1][j];
int val;
for(i=triangle.size()-2;i>0;i--)
{
for(j=0;j<triangle[i].size();j++)
{
dp[j]=triangle[i][j]+min(dp[j],dp[j+1]);
}
}
return triangle[0][0]+min(dp[0],dp[1]);
}
};