diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index a146cc8b..91cd2c8b 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -266,5 +266,21 @@ var minCostClimbingStairs = function(cost) { }; ``` +### C +```c +int minCostClimbingStairs(int* cost, int costSize){ + //开辟dp数组,大小为costSize + int *dp = (int *)malloc(sizeof(int) * costSize); + //初始化dp[0] = cost[0], dp[1] = cost[1] + dp[0] = cost[0], dp[1] = cost[1]; + + int i; + for(i = 2; i < costSize; ++i) { + dp[i] = (dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]) + cost[i]; + } + //选出倒数2层楼梯中较小的 + return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]; +} +``` -----------------------