Why input [[1]] should return 1?


  • 0
    F

    My code return 2, but I think it is correct, if the input is 1*1 grid filled with number 1, the result should be 2. But OJ tell me the correct answer is 1, why?

    class Solution {
    public:
        int minPathSum(vector<vector<int> > &grid) {
            int m=grid.size();
            int n=grid[0].size();
            int b[m+1][n+1];
            b[0][0]=0;
            for(int i=1;i<m+1;i++){
                b[i][0]=b[i-1][0]+grid[i-1][0];
            }
            for(int i=1;i<n+1;i++){
                b[0][i]=b[0][i-1]+grid[0][i-1];
            }
            for(int i=1;i<m+1;i++){
                for(int j=1;j<n+1;j++){
                    b[i][j]=min(b[i-1][j],b[i][j-1])+grid[i-1][j-1];
                }
            }
            return b[m][n];
        }
    };

  • 0
    S

    There is only 1 in the board, why do you think answer should be 2?


Log in to reply
 

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