mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:26:27 +08:00
Add Coin change ii in C code (#834)
* Update vector.h 增加功能列表: 获取向量的第 i 个元素 设置向量的第 i 个元素 向量扩容 向量缩容 向量插入元素 向量删除元素 向量交换元素 向量是否为空 向量是否已满 向量是否相等 对向量内部进行排序(升序/降序) 对向量某段数据排序(升序/降序) * Create hanota.c * 新增binary_search_recur.c * Update vector.h * Delete codes/c/chapter_divide_and_conquer directory * Update vector.h * Create binary_search_recur.c * Delete codes/chapter_divide_and_conquer directory * Update vector.h * old vector.h * Create coin_change_ii.c * Update coin_change_ii.c * Create CMakeLists.txt * Update coin_change_ii.c --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
parent
8b4d3300c3
commit
2b59c9ce88
2 changed files with 72 additions and 0 deletions
|
@ -3,3 +3,4 @@ add_executable(min_path_sum min_path_sum.c)
|
|||
add_executable(knapsack knapsack.c)
|
||||
add_executable(unbounded_knapsack unbounded_knapsack.c)
|
||||
add_executable(coin_change coin_change.c)
|
||||
add_executable(coin_change_ii coin_change_ii.c)
|
||||
|
|
71
codes/c/chapter_dynamic_programming/coin_change_ii.c
Normal file
71
codes/c/chapter_dynamic_programming/coin_change_ii.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* File: coin_change_ii.c
|
||||
* Created Time: 2023-10-02
|
||||
* Author: Zuoxun (845242523@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 零钱兑换 II:动态规划 */
|
||||
int coinChangeIIDP(int coins[], int amt, int coinsSize) {
|
||||
int n = coinsSize;
|
||||
// 初始化 dp 表
|
||||
int dp[n + 1][amt + 1];
|
||||
memset(dp, 0, sizeof(dp));
|
||||
// 初始化首列
|
||||
for (int i = 0; i <= n; i++) {
|
||||
dp[i][0] = 1;
|
||||
}
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超过背包容量,则不选硬币 i
|
||||
dp[i][a] = dp[i - 1][a];
|
||||
} else {
|
||||
// 不选和选硬币 i 这两种方案之和
|
||||
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n][amt];
|
||||
}
|
||||
|
||||
/* 零钱兑换 II:空间优化后的动态规划 */
|
||||
int coinChangeIIDPComp(int coins[], int amt, int coinsSize) {
|
||||
int n = coinsSize;
|
||||
// 初始化 dp 表
|
||||
int dp[amt + 1];
|
||||
memset(dp, 0, sizeof(dp));
|
||||
dp[0] = 1;
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超过背包容量,则不选硬币 i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// 不选和选硬币 i 这两种方案之和
|
||||
dp[a] = dp[a] + dp[a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt];
|
||||
}
|
||||
|
||||
/* Driver code */
|
||||
int main() {
|
||||
int coins[] = {1, 2, 5};
|
||||
int coinsSize = sizeof(coins) / sizeof(coins[0]);
|
||||
int amt = 5;
|
||||
|
||||
// 动态规划
|
||||
int res = coinChangeIIDP(coins, amt, coinsSize);
|
||||
printf("凑出目标金额的硬币组合数量为 %d\n", res);
|
||||
|
||||
// 空间优化后的动态规划
|
||||
res = coinChangeIIDPComp(coins, amt, coinsSize);
|
||||
printf("凑出目标金额的硬币组合数量为 %d\n", res);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue