-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0063.go
45 lines (40 loc) · 889 Bytes
/
0063.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
if len(obstacleGrid) == 0 || len(obstacleGrid[0]) == 0 {
return 0
}
m := len(obstacleGrid)
n := len(obstacleGrid[0])
if obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1 {
return 0
}
res := make([][]int, m)
for x := 0; x < m; x++ {
xArr := make([]int, n)
res[x] = xArr
for y := 0; y < n; y++ {
if x == 0 && y == 0 {
xArr[y] = 1
} else if x == 0 {
if obstacleGrid[x][y] == 1 || res[x][y-1] == 0 {
xArr[y] = 0
} else {
xArr[y] = 1
}
} else if y == 0 {
if obstacleGrid[x][y] == 1 || res[x-1][y] == 0 {
xArr[y] = 0
} else {
xArr[y] = 1
}
} else {
if obstacleGrid[x][y] == 1 || (res[x-1][y] == 0 && res[x][y-1] == 0) {
xArr[y] = 0
} else {
xArr[y] = res[x-1][y]+res[x][y-1]
}
}
}
}
return res[m-1][n-1]
}