Merge pull request #1271 from xiaofei-2020/dp04

添加(0746.使用最小花费爬楼梯.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-05-18 09:28:50 +08:00
committed by GitHub

View File

@ -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
```c ```c
int minCostClimbingStairs(int* cost, int costSize){ int minCostClimbingStairs(int* cost, int costSize){
//开辟dp数组大小为costSize //开辟dp数组大小为costSize