Leetcode 120. 三角形最小路径和(DAY 28) ---- 动态规划学习期

原题题目

Leetcode 120. 三角形最小路径和(DAY 28) ---- 动态规划学习期



代码实现(首刷自解)

int minimumTotal(int** triangle, int triangleSize, int* triangleColSize){
    int y = triangleSize,x = triangleColSize[0],i,j,min = INT_MAX;
    for(i=0;i<y;i++)
    {
        for(j=0;j<triangleColSize[i];j++)
        {
            if(!i)
                triangle[i][j] = triangle[i][j];
            else if(!j)
                triangle[i][j] = triangle[i-1][j] + triangle[i][j];
            else if(i == j)
                triangle[i][j] = triangle[i-1][j-1] + triangle[i][j];
            else
                triangle[i][j] = fmin(triangle[i-1][j-1],triangle[i-1][j]) + triangle[i][j];
            if(i == y-1)
            {
                if(triangle[i][j] < min)
                    min = triangle[i][j];
            }
        }
    }
    return min;
}
上一篇:LeetCode120. 三角形最小路径和


下一篇:Triangle