Follow up for "Unique Paths":
Now consider if some obstacles is added to the grids. How many unique paths would there be?
An obstacle and empty space are marked as and respectively in the 1
0
grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[ [0,0,0], [0,1,0], [0,0,0]
The total number of unique paths is 2
.
Note: m and N would be is at most 100.
Problem Solving Ideas:
If you use the solution one in the problem, it will be very complex, so we can modify the solution two, Java implementation is as follows:
public int uniquepathswithobstacles (int[][] obstaclegrid) { int[] v = new Int[obstaclegrid[0].length];for (int i = 0; i < v.length; i++) if (obstaclegrid[0][i] = = 0) V[i] = 1;elsebreak;for (int i = 1; i < obstaclegrid.length; i++) {if (obstaclegrid[i][0 ] = = 1) v[0] = 0;for (int j = 1; J < V.length, J + +) if (obstaclegrid[i][j] = 1) v[j] = 0;elsev[j] + = v[j-1];} return v[v.length-1]; }
Java for Leetcode 063 Unique Paths II