File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ ## Description
2+
3+ DP๋ฅผ ์ด์ฉํ์ฌ ํ์ดํ ์ ์์ต๋๋ค.
4+
5+ ๋ฐฐ์ด ` memo ` ๋ฅผ ์๋์ ๊ฐ์ด ์ ์ํฉ๋๋ค.
6+
7+ ```
8+ memo[i] = i์์ ๋ง๋ค๊ธฐ ์ํด ํ์ํ ๋์ ์ ์ต์ ๊ฐ์
9+ ๊ฐ ์์์ ๊ฐ์ ์ด๊ธฐ๊ฐ์ 10^4 + 1๋ก ์ค์ ํจ (max(amount) / min(coin) + 1)
10+ ```
11+
12+ ์์ ์ ์ํ ๋ฐฐ์ด ` memo ` ๋ฅผ ์ด์ฉํ๋ฉด ์๋ ์ ํ์์ด ์ฑ๋ฆฝํฉ๋๋ค.
13+
14+ ```
15+ memo[i] = min(memo[i], memo[i - coin] + 1) if i - coin >= 0
16+ ```
17+
18+ ## Big-O
19+
20+ ๋ฐฐ์ด ` coins ` ์ ํฌ๊ธฐ๋ฅผ ` N ` , ์ ์ ` amount ` ์ ํฌ๊ธฐ๋ฅผ ` K ` ๋ผ๊ณ ํ์ ๋,
21+
22+ Time complexity: ` O(N * M) `
23+
24+ Space complexity: ` O(M) `
25+
26+ ---
27+
28+ ``` cpp
29+ class Solution {
30+ public:
31+ int coinChange(vector<int >& coins, int amount) {
32+ int MAX = 10000 + 1;
33+ vector<int > memo(amount + 1, MAX);
34+ memo[ 0] = 0;
35+
36+ for (int i = 1; i <= amount; i++) {
37+ for (auto coin : coins) {
38+ if (i - coin >= 0) {
39+ memo[i] = min(memo[i], memo[i - coin] + 1);
40+ }
41+ }
42+ }
43+
44+ return memo[amount] == MAX ? -1 : memo[amount];
45+ }
46+ };
47+ ```
You canโt perform that action at this time.
0 commit comments