Something goes wrong with my code!

  • 0

    class Solution {

    public:int PathHelper(vector<vector<int> > &grid,int x,int y,int sum) {

       if(x>grid[0].size()-1 || y>grid.size()-1)
        return INT_MAX;
        if(x==grid[0].size()-1 && y==grid.size()-1)
        return sum;
            return min(PathHelper(grid,x+1,y,sum),PathHelper(grid,x,y+1,sum));
    int minPathSum(vector<vector<int> > &grid) {
        return PathHelper(grid,0,0,0);


    I encounter vector subscript out of range problem but i can't figure out why it that.

  • 0

    Can you explain your code?
    I cannot understand about the

    return min(PathHelper(grid,x+1,y,sum),PathHelper(grid,x,y+1,sum));

    Above all , I don't think this problem can be solved by recursion .

    Because it will do a lot of same recursion.
    And will not pass the time limit.

    You can look up to the Climbing Stairs problem.

Log in to reply

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