Ruby O(n) elegant solution. One line modification from original Unique Paths question


  • 0
    S
    def unique_paths_with_obstacles(obstacle_grid)
        m = obstacle_grid.length
        n = obstacle_grid.first.length
        
        result = Array.new(n+1, 0)
        result[0] = 1 if obstacle_grid[0][0] == 0
        for i in 0...m
            for j in 0...n
                result[j] += result[j-1] if j > 0
                result[j] = 0 if obstacle_grid[i][j] == 1
            end
        end
        result[n-1]
    end
    

Log in to reply
 

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