Add Min cost climbing stairs dp in C code (#828)

* 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

* Create min_cost_climbing_stairs_dp.c

* Update min_cost_climbing_stairs_dp.c

* Update min_cost_climbing_stairs_dp.c

* RollBack vector.h

* Update min_cost_climbing_stairs_dp.c

* Update min_cost_climbing_stairs_dp.c

* Create CMakeLists.txt

* Update min_cost_climbing_stairs_dp.c
pull/830/head^2
王作勋 1 year ago committed by GitHub
parent 21fa72ea9f
commit c6bc10a101
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1 @@
add_executable(min_cost_climbing_stairs_dp min_cost_climbing_stairs_dp.c)

@ -0,0 +1,63 @@
/**
* File: min_cost_climbing_stairs_dp.c
* Created Time: 2023-10-02
* Author: Zuoxun (845242523@qq.com)
*/
#include "../utils/common.h"
/* 求最小值 */
int min(int a, int b) {
return a < b ? a : b;
}
/* 爬楼梯最小代价:动态规划 */
int minCostClimbingStairsDP(int cost[], int costSize) {
int n = costSize - 1;
if (n == 1 || n == 2)
return cost[n];
// 初始化 dp 表,用于存储子问题的解
int dp[n + 1];
// 初始状态:预设最小子问题的解
dp[1] = cost[1];
dp[2] = cost[2];
// 状态转移:从较小子问题逐步求解较大子问题
for (int i = 3; i <= n; i++) {
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
}
return dp[n];
}
/* 爬楼梯最小代价:空间优化后的动态规划 */
int minCostClimbingStairsDPComp(int cost[], int costSize) {
int n = costSize - 1;
if (n == 1 || n == 2)
return cost[n];
int a = cost[1], b = cost[2];
for (int i = 3; i <= n; i++) {
int tmp = b;
b = min(a, tmp) + cost[i];
a = tmp;
}
return b;
}
/* Driver Code */
int main() {
int cost[] = {0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1};
int costSize = sizeof(cost) / sizeof(cost[0]);
printf("输入楼梯的代价列表为 [");
for (int i = 0; i < costSize; i++) {
if (i != costSize - 1) printf("%d, ", cost[i]);
else printf("%d", cost[i]);
}
printf("]\n");
int res = minCostClimbingStairsDP(cost, costSize);
printf("爬完楼梯的最低代价为 %d\n", res);
res = minCostClimbingStairsDPComp(cost, costSize);
printf("爬完楼梯的最低代价为 %d\n", res);
return 0;
}
Loading…
Cancel
Save