leetcode 120. Triangle

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        if(triangle.empty())return 0;
        int n= triangle.size();
        int dp[n][n];
       
        dp[0][0]=triangle[0][0];
        
        for(int i=1;i<n;i++)
        {
        for(int j=0;j<i+1;j++)
        {
            int a=INT_MAX/2,b=INT_MAX/2;
            if(j<=i-1)
             a = dp[i-1][j];
             if(j>=1)
            b = dp[i-1][j-1];
            
            dp[i][j]=min(a,b)+triangle[i][j];
        
        }
        }
        
        int min=INT_MAX;
        for(int i=0;i<n;i++)
        {
            min= dp[n-1][i]<min? dp[n-1][i]:min;
        }
        return min;
       
    }
};

你可能感兴趣的:(leetcode 120. Triangle)