From 25e26f1f86bf246f93560710f41dcf5a26414770 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 1 May 2022 13:45:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md)=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) 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