Skip to content

Commit 9846b55

Browse files
authored
Merge pull request #1941 from kkr2/feat/0518-coin-change-2.go
Create 0518-coin-change-2.go
2 parents 7e18c10 + 1702f40 commit 9846b55

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: go/0518-coin-change-2.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func change(amount int, coins []int) int {
2+
3+
row := make([]int, amount+1)
4+
row[0] = 1
5+
6+
for i := len(coins) - 1; i >= 0; i-- {
7+
8+
nextRow := make([]int, amount+1)
9+
nextRow[0] = 1
10+
11+
for a := 1; a < amount+1; a++ {
12+
nextRow[a] = row[a]
13+
if a-coins[i] >= 0 {
14+
nextRow[a] += nextRow[a-coins[i]]
15+
}
16+
}
17+
18+
row = nextRow
19+
}
20+
21+
return row[amount]
22+
}
23+

0 commit comments

Comments
 (0)