LeetCode 63. Unique Paths II

Description

https://leetcode.com/problems/unique-paths-ii/description/

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as `1` and `0` respectively in the grid.

For example,

There is one obstacle in the middle of a 3×3 grid as illustrated below.

The total number of unique paths is `2`.

Note: m and n will be at most 100.

Explanation

Follow up question for Unique Paths. We can still use a two-dimensional array to show the number of unique paths to each square on the grid.

The number of unique paths to each square on the first column would be 1 if the square is not an obstacle. If the square is an obstacle, the square and the remaining squares on the first column would be 0.

The number of unique paths to each square on the first row would be 1 if the square is not an obstacle. If the square is an obstacle, the square and the remaining squares on the first row would be 0.

The number of unique paths to the squares not at the first row or first column would be the sum of the number of unique paths to its previous left square and top square if the square is not an obstacle. If the square is an obstacle, the unique path would be just 0.