|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import com.fishercoder.common.utils.CommonUtils; |
| 3 | +/** |
| 4 | + * 63. Unique Paths II |
4 | 5 |
|
5 |
| -/**63. Unique Paths II |
6 |
| - * |
7 |
| -Follow up for "Unique Paths": |
| 6 | + Follow up for "Unique Paths": |
8 | 7 |
|
9 | 8 | Now consider if some obstacles are added to the grids. How many unique paths would there be?
|
10 | 9 |
|
|
22 | 21 |
|
23 | 22 | Note: m and n will be at most 100.*/
|
24 | 23 | public class _63 {
|
| 24 | + public static class Solution1 { |
25 | 25 | /**
|
26 |
| - * Idea: grid[i][j] has to be set to zero if obstacleGrid[i][j] == 1, |
27 |
| - * otherwise, we can get dp[i][j] from its top and left dp. |
| 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 | 28 | */
|
29 | 29 | public int uniquePathsWithObstacles(int[][] obstacleGrid) {
|
30 |
| - if (obstacleGrid == null || obstacleGrid.length == 0) { |
31 |
| - return 0; |
32 |
| - } |
| 30 | + if (obstacleGrid == null || obstacleGrid.length == 0) { |
| 31 | + return 0; |
| 32 | + } |
33 | 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 |
| - } |
| 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 | + } |
44 | 44 |
|
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]; |
53 |
| - } |
54 |
| - if (obstacleGrid[i][j - 1] == 0) { |
55 |
| - paths += dp[i][j - 1]; |
56 |
| - } |
57 |
| - dp[i][j] = paths; |
58 |
| - } |
| 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]; |
| 53 | + } |
| 54 | + if (obstacleGrid[i][j - 1] == 0) { |
| 55 | + paths += dp[i][j - 1]; |
59 | 56 | }
|
| 57 | + dp[i][j] = paths; |
| 58 | + } |
60 | 59 | }
|
61 |
| - CommonUtils.printMatrix(dp); |
62 |
| - return dp[height - 1][width - 1]; |
63 |
| - } |
64 |
| - |
65 |
| - public static void main(String... strings) { |
66 |
| - _63 test = new _63(); |
67 |
| -// int[][] obstacleGrid = new int[3][3]; |
68 |
| -// obstacleGrid[0][0] = 0; |
69 |
| -// obstacleGrid[0][1] = 0; |
70 |
| -// obstacleGrid[0][2] = 0; |
71 |
| -// obstacleGrid[1][0] = 0; |
72 |
| -// obstacleGrid[1][1] = 1; |
73 |
| -// obstacleGrid[1][2] = 0; |
74 |
| -// obstacleGrid[2][0] = 0; |
75 |
| -// obstacleGrid[2][1] = 0; |
76 |
| -// obstacleGrid[2][2] = 0; |
77 |
| - |
78 |
| -// int[][] obstacleGrid = new int[1][2]; |
79 |
| -// obstacleGrid[0][0] = 1; |
80 |
| -// obstacleGrid[0][1] = 0; |
81 |
| - |
82 |
| - int[][] obstacleGrid = new int[2][2]; |
83 |
| - obstacleGrid[0][0] = 0; |
84 |
| - obstacleGrid[0][1] = 0; |
85 |
| - obstacleGrid[1][0] = 0; |
86 |
| - obstacleGrid[1][1] = 1; |
87 |
| - |
88 |
| - CommonUtils.printMatrix(obstacleGrid); |
89 |
| - System.out.println(test.uniquePathsWithObstacles(obstacleGrid)); |
| 60 | + } |
| 61 | + return dp[height - 1][width - 1]; |
90 | 62 | }
|
| 63 | + } |
91 | 64 | }
|
0 commit comments