Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 999ff02

Browse files
Add new case
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 02b1769 commit 999ff02

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

‎0518_coin_change_ii/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O1 -o test coin_change.c

‎0518_coin_change_ii/coin_change.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
int change(int amount, int* coins, int coinsSize)
6+
{
7+
int i, j;
8+
unsigned int **dp = malloc((coinsSize + 1) * sizeof(unsigned int *));
9+
10+
for (i = 0; i <= coinsSize; i++) {
11+
dp[i] = calloc(amount + 1, sizeof(unsigned int));
12+
dp[i][0] = 1;
13+
}
14+
15+
for (i = 1; i <= coinsSize; i++) {
16+
for (j = 1; j <= amount; j++) {
17+
if (j - coins[i - 1] >= 0) {
18+
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]];
19+
} else {
20+
dp[i][j] = dp[i - 1][j];
21+
}
22+
}
23+
}
24+
25+
return dp[coinsSize][amount];
26+
}
27+
28+
int main(int argc, char **argv)
29+
{
30+
if (argc < 3) {
31+
fprintf(stderr, "Usage: ./test 11 1 2 5");
32+
exit(-1);
33+
}
34+
35+
int amount = atoi(argv[1]);
36+
int i, size = argc - 2;
37+
int *coins = malloc(size * sizeof(int));
38+
for (i = 0; i < size; i++) {
39+
coins[i] = atoi(argv[i + 2]);
40+
}
41+
printf("%d\n", change(amount, coins, size));
42+
43+
return 0;
44+
}

‎0518_coin_change_ii/coin_change.cc‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int change(int amount, vector<int>& coins) {
8+
vector<vector<unsigned int>> dp(coins.size() + 1, vector<unsigned int>(amount + 1));
9+
for (int i = 0; i <= coins.size(); i++) {
10+
dp[i][0] = 1;
11+
}
12+
13+
for (int i = 1; i <= coins.size(); i++) {
14+
for (int j = 1; j <= amount; j++) {
15+
if (j >= coins[i - 1]) {
16+
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]];
17+
} else {
18+
dp[i][j] = dp[i - 1][j];
19+
}
20+
}
21+
}
22+
return dp[coins.size()][amount];
23+
}
24+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /