Created
September 22, 2018 01:20
-
-
Save yojkim/e7422a7e733b71aaa08a1ab0f0b39fb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func uniquePathsWithObstacles(obstacleGrid [][]int) int { | |
if obstacleGrid[0][0] == 1 || obstacleGrid == nil { | |
return 0 | |
} | |
obstacleGrid[0][0] = 1 | |
maxX := len(obstacleGrid[0]) | |
maxY := len(obstacleGrid) | |
for i:=1; i<maxY; i++ { | |
if obstacleGrid[i][0] == 1 { | |
obstacleGrid[i][0] = 0 | |
} else { | |
obstacleGrid[i][0] = obstacleGrid[i-1][0] | |
} | |
} | |
for i:=1; i<maxX; i++ { | |
if obstacleGrid[0][i] == 1 { | |
obstacleGrid[0][i] = 0 | |
} else { | |
obstacleGrid[0][i] = obstacleGrid[0][i-1] | |
} | |
} | |
for i:=1; i<maxY; i++ { | |
for j:=1; j<maxX; j++ { | |
if obstacleGrid[i][j] == 1 { | |
obstacleGrid[i][j] = 0 | |
} else { | |
obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1] | |
} | |
} | |
} | |
fmt.Println(obstacleGrid) | |
return obstacleGrid[maxY-1][maxX-1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment