My 24 ms , 9-Line C Code


  • 1
    U
    int minPathSum(int** grid, int gridRowSize, int gridColSize) 
    {
        int m=gridRowSize,n=gridColSize,i,j,sol[m][n];
        sol[0][0]=grid[0][0];
        for(i=1;i<n;++i)sol[0][i]=grid[0][i]+sol[0][i-1];
        for(i=1;i<m;++i)sol[i][0]=grid[i][0]+sol[i-1][0];
        for (i = 1; i<m; i++)for ( j = 1; j<n; j++)sol[i][j]=grid[i][j]+(sol[i-1][j]>sol[i][j-1]?sol[i][j-1]:sol[i-1][j]);
        return sol[m - 1][n - 1];
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.