MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / UniquePaths

Function UniquePaths

dynamic/uniquepaths.go:8–33  ·  view source on GitHub ↗

UniquePaths implements the solution to the "Unique Paths" problem

(m, n int)

Source from the content-addressed store, hash-verified

6
7// UniquePaths implements the solution to the "Unique Paths" problem
8func UniquePaths(m, n int) int {
9 if m <= 0 || n <= 0 {
10 return 0
11 }
12
13 grid := make([][]int, m)
14 for i := range grid {
15 grid[i] = make([]int, n)
16 }
17
18 for i := 0; i < m; i++ {
19 grid[i][0] = 1
20 }
21
22 for j := 0; j < n; j++ {
23 grid[0][j] = 1
24 }
25
26 for i := 1; i < m; i++ {
27 for j := 1; j < n; j++ {
28 grid[i][j] = grid[i-1][j] + grid[i][j-1]
29 }
30 }
31
32 return grid[m-1][n-1]
33}

Callers 1

TestUniquePathsFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestUniquePathsFunction · 0.68