Simple tabulation method without using extra space


#1
int Solution::minimumTotal(vector<vector<int> > &A) 
{
    if(A.empty())
    {
        return 0;
    }
    if(A.size()==1)
    {
        return A[0][0];
    }
    int n=A.size(),c=1,ans=INT_MAX;
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<A[i].size();j++)
        {
            int low=(j==0?INT_MAX:A[i-1][j-1]);
            int high=(j==A[i].size()-1?INT_MAX:A[i-1][j]);
            A[i][j]=min(low,high)+A[i][j];
            if(i==A.size()-1)
            {
                ans=min(ans,A[i][j]);
            }
        }
    }
    return ans;
}