Help!! V weird bug


  • 0
    O

    Hi,

    Can anyone help? Code below breaking on 3x3 case (if you can explain why greatly appreciated, working fine locally), however even weirder when I include a nonsense line which should only affect large case (over 3 rows), it now breaks on 2x2 case (nor does it return -1!!!).

    Thanks

    class Solution {
        public:
            int minPathSum(vector<vector<int> > &grid) {
                int m = grid.size(), n = grid[0].size(), n2;
                int mpn=1, i;
                if (m>2) return -1; //This line to show weird error
                while (mpn<m+n-1){
                    for(i=0;i<m;i++){
                        n2 = mpn-i;
                        if (n2>=n) break;
                        if (i==0) grid[i][n2] += grid[i][n2-1];
                        else if (n2==0) grid[i][n2] += grid[i-1][n2];
                        else grid[i][mpn-i] += min(grid[i-1][n2], grid[i][n2-1]);
                    }
                    mpn++;
                }
                return grid[m-1][n-1];
            }
        };

Log in to reply
 

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