Skip to content

Commit 2a438dc

Browse files
committed
Solve 'Unique Paths' problems
1 parent 8e74102 commit 2a438dc

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.sean.recursive;
2+
3+
import java.util.Arrays;
4+
5+
/***
6+
* 62. Unique Paths
7+
*/
8+
public class UniquePaths {
9+
private int[][] cache;
10+
public int uniquePaths(int m, int n) {
11+
12+
if(cache == null) {
13+
cache = new int[m][n];
14+
for(int i = 0; i < m; i++) {
15+
Arrays.fill(cache[i], -1);
16+
}
17+
}
18+
19+
if(m >= 1 && n >= 1 && cache[m -1][n - 1] >= 0) {
20+
return cache[m -1][n - 1];
21+
}
22+
23+
if(m <= 0 || n <= 0)
24+
return 0;
25+
26+
if(m == 1 && n == 1)
27+
return 1;
28+
29+
int up = uniquePaths(m -1, n);
30+
int left = uniquePaths(m, n - 1);
31+
32+
if(m - 1 > 0 && n > 0) {
33+
cache[m - 2][n - 1] = up;
34+
}
35+
36+
if(m > 0 && n - 1 > 0) {
37+
cache[m - 1][n - 2] = left;
38+
}
39+
return up + left;
40+
}
41+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.sean.recursive;
2+
3+
import java.util.Arrays;
4+
5+
/** * 63. Unique Paths II */
6+
public class UniquePaths2 {
7+
private int[][] cache;
8+
9+
/**
10+
* * Counts the unique path for an obstacle Grid
11+
*
12+
* @param m width of grid
13+
* @param n height of grid
14+
* @param obstacleGrid two-dimensional array
15+
* @return number of unique path from [0, 0] -> [m-1, n-1]
16+
*/
17+
private int uniquePaths(int m, int n, int[][] obstacleGrid) {
18+
19+
if (cache == null) {
20+
cache = new int[m][n];
21+
for (int i = 0; i < m; i++) {
22+
Arrays.fill(cache[i], -1);
23+
}
24+
}
25+
26+
if (m >= 1 && n >= 1 && cache[m - 1][n - 1] >= 0) {
27+
return cache[m - 1][n - 1];
28+
}
29+
30+
if (m <= 0 || n <= 0) return 0;
31+
32+
// check the start point
33+
if (m == 1 && n == 1) return obstacleGrid[m - 1][n - 1] == 1 ? 0 : 1;
34+
35+
if (obstacleGrid[m - 1][n - 1] == 1) {
36+
// blocked node
37+
return 0;
38+
}
39+
40+
int up = uniquePaths(m - 1, n, obstacleGrid);
41+
int left = uniquePaths(m, n - 1, obstacleGrid);
42+
43+
if (m - 1 > 0 && n > 0) {
44+
cache[m - 2][n - 1] = up;
45+
}
46+
47+
if (m > 0 && n - 1 > 0) {
48+
cache[m - 1][n - 2] = left;
49+
}
50+
return up + left;
51+
}
52+
53+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
54+
if (obstacleGrid == null || obstacleGrid.length == 0) return 0;
55+
56+
return uniquePaths(obstacleGrid.length, obstacleGrid[0].length, obstacleGrid);
57+
}
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.sean.recursive;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
public class UniquePaths2Test {
8+
9+
private UniquePaths2 uniquePaths2;
10+
11+
@Before
12+
public void setUp() {
13+
uniquePaths2 = new UniquePaths2();
14+
}
15+
16+
@Test
17+
public void uniquePathsWithObstacles() {
18+
int count =
19+
uniquePaths2.uniquePathsWithObstacles(
20+
new int[][] {
21+
new int[] {0, 0, 0},
22+
new int[] {0, 1, 0},
23+
new int[] {0, 0, 0}
24+
});
25+
26+
Assert.assertEquals(2, count);
27+
}
28+
29+
@Test
30+
public void testEmptyPath() {
31+
int count = uniquePaths2.uniquePathsWithObstacles(new int[][] {new int[] {1}});
32+
33+
Assert.assertEquals(0, count);
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.sean.recursive;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class UniquePathsTest {
10+
11+
private UniquePaths paths;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
paths = new UniquePaths();
16+
}
17+
18+
@Test
19+
public void uniquePaths() {
20+
Assert.assertEquals(3, paths.uniquePaths(3,2));
21+
}
22+
}

0 commit comments

Comments
 (0)