C 0ms DP


  • 0
    Y
    #define MAX 1000
    int p[MAX][MAX]= {0};
    
    int minPathSum(int** grid, int gridRowSize, int gridColSize) {
        int i,j,s=0;
        for(i=0;i<gridRowSize;i++){
                s += grid[i][0];
                p[i][0] = s;
        }
        s = 0;
        for(i=0;i<gridColSize;i++){
                s += grid[0][i];
                p[0][i] = s;
        }
        for(i=1;i<gridRowSize;i++){
                for(j=1;j<gridColSize;j++){
                        if(p[i][j-1]<p[i-1][j])
                                p[i][j] = p[i][j-1]+grid[i][j];
                        else
                                p[i][j] = p[i-1][j]+grid[i][j];
                }
        }
        return p[gridRowSize-1][gridColSize-1];
    }

Log in to reply
 

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