From 16942dfe321b04a9e6a09116703d88dbf5e8e91b Mon Sep 17 00:00:00 2001 From: jiawenshi <50801603+qq909244296@users.noreply.github.com> Date: Sun, 14 Apr 2024 17:30:21 +0800 Subject: [PATCH] fix(C): fix the array initialization in coin_change.c (#1277) * add * Update coin_change.c * Update coin_change.c --------- Co-authored-by: Yudong Jin --- codes/c/chapter_dynamic_programming/coin_change.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/codes/c/chapter_dynamic_programming/coin_change.c b/codes/c/chapter_dynamic_programming/coin_change.c index d700eeb9f..0bd9d1ae3 100644 --- a/codes/c/chapter_dynamic_programming/coin_change.c +++ b/codes/c/chapter_dynamic_programming/coin_change.c @@ -50,8 +50,12 @@ int coinChangeDPComp(int coins[], int amt, int coinsSize) { int n = coinsSize; int MAX = amt + 1; // 初始化 dp 表 - int *dp = calloc(amt + 1, sizeof(int)); + int *dp = malloc((amt + 1) * sizeof(int)); + for (int j = 1; j <= amt; j++) { + dp[j] = MAX; + } dp[0] = 0; + // 状态转移 for (int i = 1; i <= n; i++) { for (int a = 1; a <= amt; a++) {