Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created June 30, 2020 08:11
Show Gist options
  • Select an option

  • Save alldroll/bee505cc64c64dff5cdfc373cc8c53d4 to your computer and use it in GitHub Desktop.

Select an option

Save alldroll/bee505cc64c64dff5cdfc373cc8c53d4 to your computer and use it in GitHub Desktop.
func uniquePaths(m int, n int) int {
// we are going to memorize all previous steps
moves := make([][]int, n)
for i, _ := range moves {
moves[i] = make([]int, m)
}
// we are already located at the top-left corner.
// There is only one way to be at this cell.
moves[0][0] = 1
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
// Can we come from left cell to the right?
if j > 0 {
moves[i][j] += moves[i][j - 1]
}
// Can we come from the top cell to the down?
if i > 0 {
moves[i][j] += moves[i - 1][j]
}
}
}
// Return the total number of unique paths
return moves[n - 1][m - 1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment