Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions UniquePaths/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```go
// Use DP
// When x == 0 or y == 0 the number of path will be 1
// Other than that, the number of path will be dp[y][x - 1] + dp[y - 1][x]
// Time complexity: O(M * n)
// Space complexity: O(M * N)
// Time spend: 03:14
package main
func uniquePaths(m int, n int) int {
dp := make([][]int, m)
for i := range dp {
dp[i] = make([]int, n)
}

for y := range dp {
for x := range dp[y] {
if x == 0 || y == 0 {
dp[y][x] = 1
continue
}
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]
}
}

return dp[m - 1][n - 1]
}
```
22 changes: 22 additions & 0 deletions UniquePaths/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
```go
// Time spend: 01:07
package main
func uniquePaths(m int, n int) int {
dp := make([][]int, m)
for i := range dp {
dp[i] = make([]int, n)
}

for y := range dp {
for x := range dp[y] {
if x == 0 || y == 0 {
dp[y][x] = 1
continue
}
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]
}
}

return dp[m - 1][n - 1]
}
```
68 changes: 68 additions & 0 deletions UniquePaths/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
```go
// Time spend: 01:12
package main
func uniquePaths(m int, n int) int {
dp := make([][]int, m)
for i := range dp {
dp[i] = make([]int, n)
}

for y := range dp {
for x := range dp[y] {
if x == 0 || y == 0 {
dp[y][x] = 1
continue
}
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]
}
}

return dp[m - 1][n - 1]
}
```

```go
// Time spend: 01:06
package main
func uniquePaths(m int, n int) int {
dp := make([][]int, m)
for i := range dp {
dp[i] = make([]int, n)
}

for y := range dp {
for x := range dp[y] {
if x == 0 || y == 0 {
dp[y][x] = 1
continue
}
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]
}
}

return dp[m - 1][n - 1]
}
```

```go
// Time spend: 01:08
package main
func uniquePaths(m int, n int) int {
dp := make([][]int, m)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i := range dp {
dp[i] = make([]int, n)
}

for y := range dp {
for x := range dp[y] {
if x == 0 || y == 0 {
dp[y][x] = 1
continue
}
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]
}
}

return dp[m - 1][n - 1]
}
```