diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index c356955a..5931fc8a 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -266,7 +266,30 @@ var minCostClimbingStairs = function(cost) { }; ``` +### TypeScript + +```typescript +function minCostClimbingStairs(cost: number[]): number { + /** + dp[i]: 走到第i阶需要花费的最少金钱 + dp[0]: cost[0]; + dp[1]: cost[1]; + ... + dp[i]: min(dp[i - 1], dp[i - 2]) + cost[i]; + */ + const dp: number[] = []; + const length: number = cost.length; + dp[0] = cost[0]; + dp[1] = cost[1]; + for (let i = 2; i <= length; i++) { + dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i]; + } + return Math.min(dp[length - 1], dp[length - 2]); +}; +``` + ### C + ```c int minCostClimbingStairs(int* cost, int costSize){ //开辟dp数组,大小为costSize