Simple Java O(mn) solution (1ms) with DP


  • 0
    public class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            if (obstacleGrid.length == 0 || obstacleGrid[0].length == 0)
    			return 0;
    		int m = obstacleGrid.length,
    			n = obstacleGrid[0].length;
    		int[][] f = new int[m][n];
    		int i = 0, j = 0;
    		while (i < m && obstacleGrid[i][0] == 0) {
    			f[i][0] = 1;
    			i ++;
    		}
    		while (j < n && obstacleGrid[0][j] == 0) {
    			f[0][j] = 1;
    			j ++;
    		}
    		for (i = 1; i < m; i ++)
    			for (j = 1; j < n; j ++)
    				if (obstacleGrid[i][j] == 0) 
    					f[i][j] = f[i - 1][j] + f[i][j - 1]; 
    		return f[m - 1][n - 1];
        }
    }

Log in to reply
 

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