-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path62_unique_paths.go
More file actions
32 lines (29 loc) · 803 Bytes
/
62_unique_paths.go
File metadata and controls
32 lines (29 loc) · 803 Bytes
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
package leetcode
// https://leetcode.com/problems/unique-paths/description/
func uniquePaths(m int, n int) int {
paths := make([][]int, m+1)
for i := 0; i < m+1; i++ {
paths[i] = make([]int, n+1)
}
paths[1][0] = 1
for i := 1; i < m+1; i++ {
for j := 1; j < n+1; j++ {
paths[i][j] = paths[i-1][j] + paths[i][j-1]
}
}
return paths[m][n]
}
// We can actually do this with 1D array
// Notice in the path, we need paths[i-1][j] + paths[i][j-1]
// instead of paths[i-1][j], we can reuse the CURRENT cell to store the result
// paths[j] = paths[j] + paths[j-1], because paths[j] is storing result of i-1,j already
func uniquePaths1D(m int, n int) int {
dp := make([]int, n)
dp[0] = 1
for i := 0; i < m; i++ {
for j := 1; j < n; j++ {
dp[j] = dp[j] + dp[j-1]
}
}
return dp[n-1]
}