Skip to content

Commit a60bfb0

Browse files
committed
4주차 문제 풀이 1개 추가
- Coin Change
1 parent bf2a30b commit a60bfb0

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

coin-change/hwi-middle.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int coinChange(vector<int>& coins, int amount) {
4+
if (amount < 1)
5+
{
6+
return 0;
7+
}
8+
9+
vector<int> v(amount);
10+
return solve(coins, amount, v);
11+
}
12+
13+
int solve(vector<int>& coins, int amount, vector<int>& count)
14+
{
15+
if (amount < 0) return -1;
16+
if (amount == 0) return 0;
17+
if (count[amount - 1] != 0) return count[amount - 1];
18+
19+
int min = INT_MAX;
20+
for (int coin : coins)
21+
{
22+
int res = solve(coins, amount - coin, count); // coin을 사용
23+
if (res >= 0 && res < min) // 결과가 유효하고 현재 최솟값보다 작으면 업데이트
24+
{
25+
min = res + 1; // coin을 미리 사용하고 찾은 값이므로 1 더함
26+
}
27+
}
28+
29+
count[amount - 1] = (min == INT_MAX) ? -1 : min;
30+
return count[amount - 1];
31+
}
32+
};

0 commit comments

Comments
 (0)