518. Coin Change 2 (Unbounded Knapsack problem)

https://labuladong.github.io/algo/3/27/83/

https://labuladong.github.io/algo/3/27/83/

DFS

https://leetcode-cn.com/problems/coin-change-2/solution/custerxue-xi-bi-ji-dfs-dp-by-custergo/

DFS + memo

上個做法用了 combination 的想法 i = start

也可以這樣做, 這樣做就跟完全背包的概念一樣

start + 1 也可以避開重複使用 coin 的 case

因為我們要的結果是 solutions, 我們要的是 combination, 所以 要避免重複用 1 (以下面的例子來說)

DP - 2D - Unbounded Knapsack problem

DP - space 優化到 O(n)

因為dp 数组的转移只和 dp[i][..]dp[i-1][..] 有关, i 和 i -1

from 2D to 1D, 去掉 i 的部分

for (int coin : coins) loop 外什麼一定要寫在外層 loop呢? 為了 排列唯一

那因為 coin 只是遍例, 所以用 for (int coin : coins) 就可以了

dp 部分也可以改成 +=

time: O(coin * amount)

space: O(amount)

Last updated

Was this helpful?