lc-go/solutions/0/q63/solution.go
2026-01-04 14:44:06 +09:00

28 lines
634 B
Go

package q63
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
w, h := len(obstacleGrid[0]), len(obstacleGrid)
if obstacleGrid[h-1][w-1]+obstacleGrid[0][0] > 0 {
return 0
}
obstacleGrid[h-1][w-1] = -1
for i := w + h - 2; i >= 0; i-- {
for x := min(i, w-1); x >= 0 && i-x < h; x-- {
y := i - x
if obstacleGrid[y][x] == 1 {
continue
}
if x+1 < w && obstacleGrid[y][x+1] != 1 {
obstacleGrid[y][x] += obstacleGrid[y][x+1]
}
if y+1 < h && obstacleGrid[y+1][x] != 1 {
obstacleGrid[y][x] += obstacleGrid[y+1][x]
}
}
}
return -1 * obstacleGrid[0][0]
}
var _ = uniquePathsWithObstacles