File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ bottom-up dp 활용, 낮은 수부터 amount까지 차례로 해당 금액을 만들 수 있는 최소 동전 개수를 업데이트.
4
+ amount가 0이면 동전 개수 0이므로 dp[0] = 0으로 초기화
5
+ 그 외의 초기값은 amount + 1로 설정
6
+ (1짜리 동전으로 채우면 dp[amount] 최댓값 == amount이므로 amount + 1 그대로이면 채울 수 없는 케이스)
7
+
8
+ coin 종류 : C, amount 크기 : A
9
+
10
+ TC : O(A * C)
11
+ amount의 크기 * coin 종류만큼 반복문
12
+
13
+ SC : O(A)
14
+ dp배열의 크기는 amount 크기에 비례
15
+ */
16
+
17
+ class Solution {
18
+ public:
19
+ int coinChange (vector<int >& coins, int amount) {
20
+ vector<int > dp (amount + 1 , amount + 1 );
21
+
22
+ dp[0 ] = 0 ;
23
+ for (int i = 0 ; i <= amount; i++)
24
+ {
25
+ for (auto coin : coins)
26
+ {
27
+ if (i - coin >= 0 )
28
+ dp[i] = min (dp[i - coin] + 1 , dp[i]);
29
+ }
30
+ }
31
+ if (dp[amount] == amount + 1 )
32
+ return -1 ;
33
+ else
34
+ return dp[amount];
35
+ }
36
+ };
You can’t perform that action at this time.
0 commit comments