Skip to content

Commit 5114d8c

Browse files
Add files via upload
1 parent 44a685f commit 5114d8c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Coin Change/Coin_Change.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 典型的动态规划
2+
// Runtime: 36 ms, faster than 88.91% of C++ online submissions for Coin Change.
3+
// Memory Usage: 12.6 MB, less than 86.27% of C++ online submissions for Coin Change.
4+
class Solution
5+
{
6+
public:
7+
int coinChange(vector<int>& coins, int amount)
8+
{
9+
vector<int> memo(amount + 1, 0);
10+
for (int i = 1; i <= amount; ++i)
11+
{
12+
int minValue = INT_MAX;
13+
bool found = false;
14+
for (int coin : coins)
15+
{
16+
if (i - coin < 0 || memo[i - coin] == -1)
17+
continue;
18+
else
19+
{
20+
minValue = min(minValue, memo[i - coin] + 1);
21+
found = true;
22+
}
23+
}
24+
memo[i] = found ? minValue : -1;
25+
}
26+
return memo[amount];
27+
}
28+
};

0 commit comments

Comments
 (0)