why my DFS solution get TLE?


  • 0
    J

    class Solution {
    public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
    int couint = 0;
    if (obstacleGrid.size() == 0) {return 0;}
    DFS(couint, obstacleGrid, 1, 1, obstacleGrid.size(), obstacleGrid[0].size(), obstacleGrid[0].size()*obstacleGrid.size());
    return couint;
    }
    private:
    void DFS(int &couint, std::vector<std::vector<int> > obstacleGrid, int cr, int cc, int row, int col, int aim) {
    if (cr == row && cc == col && obstacleGrid[cr-1][cc-1] == 0) {
    couint++;
    return;

    	}
    	if (obstacleGrid[cr-1][cc-1] == 1) {return;}
    	
    	if (cc+1<=col && obstacleGrid[cr-1][cc] == 0) {
    		DFS(couint, obstacleGrid, cr, cc+1, row, col, aim);
    	}
    	if (cr+1<= row && obstacleGrid[cr][cc-1] == 0) {
    		DFS(couint, obstacleGrid, cr+1, cc, row, col, aim);
    	}
    }
    

    };


Log in to reply
 

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