Skip to content

Commit 1db9f9a

Browse files
author
Lei Cao
committed
62 unique path II
1 parent 00ba8ad commit 1db9f9a

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ LeetCode
174174
|65|[Add Binary](https://oj.leetcode.com/problems/add-binary/)| [C++](./algorithms/addBinary/addBinary.cpp)|Easy|
175175
|64|[Merge Two Sorted Lists](https://oj.leetcode.com/problems/merge-two-sorted-lists/)| [C++](./algorithms/mergeTwoSortedList/mergeTwoSortedList.cpp)|Easy|
176176
|63|[Minimum Path Sum](https://oj.leetcode.com/problems/minimum-path-sum/)| [C++](./algorithms/minimumPathSum/minimumPathSum.cpp)|Medium|
177-
|62|[Unique Paths II](https://oj.leetcode.com/problems/unique-paths-ii/)| [C++](./algorithms/uniquePaths/uniquePaths.II.cpp)|Medium|
177+
|62|[Unique Paths II](https://oj.leetcode.com/problems/unique-paths-ii/)| [C++](./algorithms/uniquePaths/uniquePaths.II.cpp), [Java](./algorithms-java/src/dynamicProgramming/uniquePaths/uniquePathsII.java)|Medium|
178178
|61|[Unique Paths](https://oj.leetcode.com/problems/unique-paths/)| [C++](./algorithms/uniquePaths/uniquePaths.cpp), [Java](./algorithms-java/src/dynamicProgramming/uniquePaths/uniquePaths.java)|Medium|
179179
|60|[Rotate List](https://oj.leetcode.com/problems/rotate-list/)| [C++](./algorithms/rotateList/rotateList.cpp)|Medium|
180180
|59|[Permutation Sequence](https://oj.leetcode.com/problems/permutation-sequence/)| [C++](./algorithms/permutationSequence/permutationSequence.cpp)|Medium|
@@ -254,4 +254,4 @@ LeetCode
254254
| # | Title | Solution | Difficulty |
255255
|---| ----- | -------- | ---------- |
256256
|1|[Search in a big sorted array](http://www.lintcode.com/en/problem/search-in-a-big-sorted-array/)|[Java](./algorithms-java/src/searchInABigSortedArray/searchInABigSortedArray.java)|Medium|
257-
[2][Search Range in Binary Search Tree](http://www.lintcode.com/en/problem/search-range-in-binary-search-tree/) | [Java](./algorithms-java/src/searchRangeInBinarySearchTree/searchRangeInBinarySearchTree.java)|Medium|
257+
|2|[Search Range in Binary Search Tree](http://www.lintcode.com/en/problem/search-range-in-binary-search-tree/) | [Java](./algorithms-java/src/searchRangeInBinarySearchTree/searchRangeInBinarySearchTree.java)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Source : https://oj.leetcode.com/problems/unique-paths-ii/
2+
// Inspired by : http://www.jiuzhang.com/solutions/unique-paths/
3+
// Author : Lei Cao
4+
// Date : 2015-10-11
5+
6+
/**********************************************************************************
7+
*
8+
* Follow up for "Unique Paths":
9+
*
10+
* Now consider if some obstacles are added to the grids. How many unique paths would there be?
11+
*
12+
* An obstacle and empty space is marked as 1 and 0 respectively in the grid.
13+
*
14+
* For example,
15+
* There is one obstacle in the middle of a 3x3 grid as illustrated below.
16+
*
17+
* [
18+
* [0,0,0],
19+
* [0,1,0],
20+
* [0,0,0]
21+
* ]
22+
*
23+
* The total number of unique paths is 2.
24+
*
25+
* Note: m and n will be at most 100.
26+
*
27+
**********************************************************************************/
28+
29+
package dynamicProgramming.uniquePaths;
30+
31+
public class uniquePathsII {
32+
/**
33+
* @param obstacleGrid: A list of lists of integers
34+
* @return: An integer
35+
*/
36+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
37+
if (obstacleGrid.length == 0 || obstacleGrid[0].length ==0) {
38+
return 0;
39+
}
40+
if (obstacleGrid[0][0] == 1) {
41+
return 0;
42+
}
43+
int m = obstacleGrid.length;
44+
int n = obstacleGrid[0].length;
45+
// write your code here
46+
int[][] matrix = new int[m][n];
47+
for (int i = 0; i < m; i++) {
48+
if (obstacleGrid[i][0] != 1) {
49+
matrix[i][0] = 1;
50+
} else {
51+
break;
52+
}
53+
}
54+
for (int i = 0; i < n; i++) {
55+
if (obstacleGrid[0][i] != 1) {
56+
matrix[0][i] = 1;
57+
} else {
58+
break;
59+
}
60+
}
61+
62+
for (int i = 1; i < m; i++) {
63+
for (int j = 1; j < n; j++) {
64+
if (obstacleGrid[i][j] == 1) {
65+
matrix[i][j] = 0;
66+
} else {
67+
matrix[i][j] = matrix[i-1][j] + matrix[i][j-1];
68+
}
69+
}
70+
}
71+
return matrix[m-1][n-1];
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dynamicProgramming.uniquePaths;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Created by leicao on 11/10/15.
9+
*/
10+
public class uniquePathsIITest {
11+
12+
@Test
13+
public void testUniquePathsWithObstacles() throws Exception {
14+
int[][][] inputs = {
15+
{
16+
{0,0,0},
17+
{0,1,0},
18+
{0,0,0},
19+
}
20+
};
21+
int[] results = {2};
22+
for (int i = 0; i < inputs.length; i++) {
23+
uniquePathsII u = new uniquePathsII();
24+
int r = u.uniquePathsWithObstacles(inputs[i]);
25+
System.out.println(r);
26+
assertEquals(results[i], r);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)