Skip to content

Commit 7d4e5fb

Browse files
committed
feat(solutions): add unique_paths_ii
1 parent d09f59c commit 7d4e5fb

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
| # | Problem | Solution | Difficulty | Single Repetition Duration | LeetCode Run Time |
2626
| ---: | :----- | :--------: | :----------: | ----------: | ----------: |
27+
|63|[Unique Paths II][Solutions-63]|[WindomZ][Solutions-63-Go]|Medium|[36.3 ns/op/5 test cases][Solutions-63-Test]|? ms|
2728
|62|[Unique Paths][Solutions-62]|[WindomZ][Solutions-62-Go]|Medium|[9.39 ns/op/10 test cases][Solutions-62-Test]|? ms|
2829
|61|[Rotate List][Solutions-61]|[WindomZ][Solutions-61-Go]|Medium|[34.0 ns/op/2 test cases][Solutions-61-Test]|? ms|
2930
|60|[Permutation Sequence][Solutions-60]|[WindomZ][Solutions-60-Go]|Medium|[73.9 ns/op/6 test cases][Solutions-60-Test]|? ms|
@@ -116,6 +117,9 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
116117
### Support
117118
If you like it then you can put a :star:Star on it.
118119

120+
[Solutions-63-Test]:solutions/unique_paths_ii/uniquepathsii_test.go#L22
121+
[Solutions-63-Go]:solutions/unique_paths_ii/uniquepathsii.go
122+
[Solutions-63]:https://leetcode.com/unique-paths-ii/
119123
[Solutions-62-Test]:solutions/unique_paths/uniquepaths_test.go#L22
120124
[Solutions-62-Go]:solutions/unique_paths/uniquepaths.go
121125
[Solutions-62]:https://leetcode.com/unique-paths/

solutions/unique_paths_ii/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 63. Unique Paths II
2+
3+
[Description](https://leetcode.com/problems/unique-paths-ii/description/) |
4+
[Discuss](https://leetcode.com/problems/unique-paths-ii/discuss/) |
5+
[Solution](https://leetcode.com/problems/unique-paths-ii/solution/)
6+
7+
## Description
8+
9+
Follow up for "Unique Paths":
10+
11+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
12+
13+
An obstacle and empty space is marked as `1` and `0` respectively in the grid.
14+
15+
For example,
16+
17+
There is one obstacle in the middle of a 3x3 grid as illustrated below.
18+
19+
```
20+
[
21+
[0,0,0],
22+
[0,1,0],
23+
[0,0,0]
24+
]
25+
```
26+
27+
The total number of unique paths is `2`.
28+
29+
**Note:** _m_ and _n_ will be at most 100.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package uniquepathsii
2+
3+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
4+
if len(obstacleGrid) == 0 {
5+
return 0
6+
}
7+
width := len(obstacleGrid[0])
8+
dp := make([]int, width)
9+
dp[0] = 1
10+
for _, row := range obstacleGrid {
11+
for col := 0; col < width; col++ {
12+
if row[col] == 1 {
13+
dp[col] = 0 // reset current paths to zero if in obstacle space
14+
} else if col > 0 {
15+
dp[col] += dp[col-1] // add up last paths if in empty space
16+
}
17+
}
18+
}
19+
return dp[width-1]
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package uniquepathsii
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_uniquePathsWithObstacles(t *testing.T) {
10+
assert.Equal(t, 0,
11+
uniquePathsWithObstacles([][]int{}))
12+
assert.Equal(t, 2,
13+
uniquePathsWithObstacles([][]int{{0, 0}, {0, 0}}))
14+
assert.Equal(t, 3,
15+
uniquePathsWithObstacles([][]int{{0, 0}, {0, 0}, {0, 0}}))
16+
assert.Equal(t, 6,
17+
uniquePathsWithObstacles([][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}))
18+
assert.Equal(t, 2,
19+
uniquePathsWithObstacles([][]int{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}))
20+
}
21+
22+
func Benchmark_uniquePathsWithObstacles(b *testing.B) {
23+
b.StopTimer()
24+
b.ReportAllocs()
25+
b.StartTimer()
26+
b.RunParallel(func(pb *testing.PB) {
27+
for pb.Next() {
28+
uniquePathsWithObstacles([][]int{})
29+
uniquePathsWithObstacles([][]int{{0, 0}, {0, 0}})
30+
uniquePathsWithObstacles([][]int{{0, 0}, {0, 0}, {0, 0}})
31+
uniquePathsWithObstacles([][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}})
32+
uniquePathsWithObstacles([][]int{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}})
33+
}
34+
})
35+
}

0 commit comments

Comments
 (0)