Skip to content

Commit

Permalink
feat: add cpp solution to lc problem: No.0518. Coin Change 2
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Jul 24, 2021
1 parent 9f0e89f commit 47333f4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
- [最长公共子序列](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
- [编辑距离](./solution/0000-0099/0072.Edit%20Distance/README.md)
- [零钱兑换](./solution/0300-0399/0322.Coin%20Change/README.md)
- [零钱兑换 II](./solution/0500-0599/0518.Coin%20Change%202/README.md)
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
- [俄罗斯套娃信封问题](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)

Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
- [Longest Common Subsequence](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md)
- [Edit Distance](./solution/0000-0099/0072.Edit%20Distance/README_EN.md)
- [Coin Change](./solution/0300-0399/0322.Coin%20Change/README_EN.md)
- [Coin Change 2](./solution/0500-0599/0518.Coin%20Change%202/README_EN.md)
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)

### Backtracking
Expand Down
22 changes: 21 additions & 1 deletion solution/0500-0599/0518.Coin Change 2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@

<!-- 这里可写通用的实现逻辑 -->

完全背包问题
动态规划。

完全背包问题。

<!-- tabs:start -->

Expand Down Expand Up @@ -123,6 +125,24 @@ func change(amount int, coins []int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1);
dp[0] = 1;
for (auto coin : coins) {
for (int j = coin; j <= amount; ++j) {
dp[j] += dp[j - coin];
}
}
return dp[amount];
}
};
```
### **...**
```
Expand Down
22 changes: 21 additions & 1 deletion solution/0500-0599/0518.Coin Change 2/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@

## Solutions

Complete knapsack problem
Dynamic programming.

Complete knapsack problem.

<!-- tabs:start -->

Expand Down Expand Up @@ -159,6 +161,24 @@ func change(amount int, coins []int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1);
dp[0] = 1;
for (auto coin : coins) {
for (int j = coin; j <= amount; ++j) {
dp[j] += dp[j - coin];
}
}
return dp[amount];
}
};
```
### **...**
```
Expand Down
13 changes: 13 additions & 0 deletions solution/0500-0599/0518.Coin Change 2/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1);
dp[0] = 1;
for (auto coin : coins) {
for (int j = coin; j <= amount; ++j) {
dp[j] += dp[j - coin];
}
}
return dp[amount];
}
};

0 comments on commit 47333f4

Please sign in to comment.