|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 63. Unique Paths II |
5 |
| -
|
6 |
| - Follow up for "Unique Paths": |
7 |
| -
|
8 |
| -Now consider if some obstacles are added to the grids. How many unique paths would there be? |
9 |
| -
|
10 |
| -An obstacle and empty space is marked as 1 and 0 respectively in the grid. |
11 |
| -
|
12 |
| -For example, |
13 |
| -There is one obstacle in the middle of a 3x3 grid as illustrated below. |
14 |
| -
|
15 |
| -[ |
16 |
| - [0,0,0], |
17 |
| - [0,1,0], |
18 |
| - [0,0,0] |
19 |
| -] |
20 |
| -The total number of unique paths is 2. |
21 |
| -
|
22 |
| -Note: m and n will be at most 100.*/ |
23 | 3 | public class _63 {
|
24 |
| - public static class Solution1 { |
25 |
| - /** |
26 |
| - * Idea: grid[i][j] has to be set to zero if obstacleGrid[i][j] == 1, otherwise, we can get |
27 |
| - * dp[i][j] from its top and left dp. |
28 |
| - */ |
29 |
| - public int uniquePathsWithObstacles(int[][] obstacleGrid) { |
30 |
| - if (obstacleGrid == null || obstacleGrid.length == 0) { |
31 |
| - return 0; |
32 |
| - } |
33 |
| - |
34 |
| - int height = obstacleGrid.length; |
35 |
| - int width = obstacleGrid[0].length; |
36 |
| - int[][] dp = new int[height][width]; |
37 |
| - dp[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1; |
38 |
| - for (int i = 1; i < height; i++) { |
39 |
| - dp[i][0] = obstacleGrid[i][0] == 1 ? 0 : dp[i - 1][0]; |
40 |
| - } |
41 |
| - for (int j = 1; j < width; j++) { |
42 |
| - dp[0][j] = obstacleGrid[0][j] == 1 ? 0 : dp[0][j - 1]; |
43 |
| - } |
| 4 | + public static class Solution1 { |
| 5 | + /** |
| 6 | + * Idea: grid[i][j] has to be set to zero if obstacleGrid[i][j] == 1, otherwise, we can get |
| 7 | + * dp[i][j] from its top and left dp. |
| 8 | + */ |
| 9 | + public int uniquePathsWithObstacles(int[][] obstacleGrid) { |
| 10 | + if (obstacleGrid == null || obstacleGrid.length == 0) { |
| 11 | + return 0; |
| 12 | + } |
44 | 13 |
|
45 |
| - for (int i = 1; i < height; i++) { |
46 |
| - for (int j = 1; j < width; j++) { |
47 |
| - if (obstacleGrid[i][j] == 1) { |
48 |
| - dp[i][j] = 0; |
49 |
| - } else { |
50 |
| - int paths = 0; |
51 |
| - if (obstacleGrid[i - 1][j] == 0) { |
52 |
| - paths += dp[i - 1][j]; |
| 14 | + int height = obstacleGrid.length; |
| 15 | + int width = obstacleGrid[0].length; |
| 16 | + int[][] dp = new int[height][width]; |
| 17 | + dp[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1; |
| 18 | + for (int i = 1; i < height; i++) { |
| 19 | + dp[i][0] = obstacleGrid[i][0] == 1 ? 0 : dp[i - 1][0]; |
53 | 20 | }
|
54 |
| - if (obstacleGrid[i][j - 1] == 0) { |
55 |
| - paths += dp[i][j - 1]; |
| 21 | + for (int j = 1; j < width; j++) { |
| 22 | + dp[0][j] = obstacleGrid[0][j] == 1 ? 0 : dp[0][j - 1]; |
| 23 | + } |
| 24 | + |
| 25 | + for (int i = 1; i < height; i++) { |
| 26 | + for (int j = 1; j < width; j++) { |
| 27 | + if (obstacleGrid[i][j] == 1) { |
| 28 | + dp[i][j] = 0; |
| 29 | + } else { |
| 30 | + int paths = 0; |
| 31 | + if (obstacleGrid[i - 1][j] == 0) { |
| 32 | + paths += dp[i - 1][j]; |
| 33 | + } |
| 34 | + if (obstacleGrid[i][j - 1] == 0) { |
| 35 | + paths += dp[i][j - 1]; |
| 36 | + } |
| 37 | + dp[i][j] = paths; |
| 38 | + } |
| 39 | + } |
56 | 40 | }
|
57 |
| - dp[i][j] = paths; |
58 |
| - } |
| 41 | + return dp[height - 1][width - 1]; |
59 | 42 | }
|
60 |
| - } |
61 |
| - return dp[height - 1][width - 1]; |
62 | 43 | }
|
63 |
| - } |
64 | 44 | }
|
0 commit comments