I use a cache named dp in my code for the visited pair(m,n)

using dfs method.

but got TLE. here is my code:

```
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m=obstacleGrid.length;
int n=obstacleGrid[0].length;
if(obstacleGrid[0][0]==1|| obstacleGrid[m-1][n-1]==1) return 0;
int[][] dp=new int[m+1][n+1];
dp[m-1][n-1]=1;
return dfs(obstacleGrid,0,0,m,n,dp);
}
private int dfs(int[][] obstacleGrid, int i, int j, int m, int n,int[][] dp) {
// TODO Auto-generated method stub
if(i>=m || j>=n) return 0;
if(dp[i][j]>0) return dp[i][j];
if(obstacleGrid[i][j]==1) return 0;
dp[i][j]=dfs(obstacleGrid,i+1,j,m,n,dp)+dfs(obstacleGrid,i,j+1,m,n,dp);
return dp[i][j];
}
```

}

what confused me is the code below is Accepted!!

```
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m=obstacleGrid.length;
int n=obstacleGrid[0].length;
if(obstacleGrid[0][0]==1|| obstacleGrid[m-1][n-1]==1) return 0;
int[][] dp=new int[m+1][n+1];
dp[0][0]=1;
return dfs(obstacleGrid,m-1,n-1,m,n,dp);
}
private int dfs(int[][] obstacleGrid, int i, int j, int m, int n,int[][] dp) {
// TODO Auto-generated method stub
if(i<0 || j<0) return 0;
if(dp[i][j]>0) return dp[i][j];
if(obstacleGrid[i][j]==1) return 0;
dp[i][j]=dfs(obstacleGrid,i-1,j,m,n,dp)+dfs(obstacleGrid,i,j-1,m,n,dp);
return dp[i][j];
}
```

}