Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 047c4db

Browse files
Merge pull request youngyangyang04#1271 from xiaofei-2020/dp04
添加(0746.使用最小花费爬楼梯.md):增加typescript版本
2 parents faeaaf4 + 25e26f1 commit 047c4db

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎problems/0746.使用最小花费爬楼梯.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,30 @@ var minCostClimbingStairs = function(cost) {
266266
};
267267
```
268268

269+
### TypeScript
270+
271+
```typescript
272+
function minCostClimbingStairs(cost: number[]): number {
273+
/**
274+
dp[i]: 走到第i阶需要花费的最少金钱
275+
dp[0]: cost[0];
276+
dp[1]: cost[1];
277+
...
278+
dp[i]: min(dp[i - 1], dp[i - 2]) + cost[i];
279+
*/
280+
const dp: number[] = [];
281+
const length: number = cost.length;
282+
dp[0] = cost[0];
283+
dp[1] = cost[1];
284+
for (let i = 2; i <= length; i++) {
285+
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
286+
}
287+
return Math.min(dp[length - 1], dp[length - 2]);
288+
};
289+
```
290+
269291
### C
292+
270293
```c
271294
int minCostClimbingStairs(int* cost, int costSize){
272295
//开辟dp数组,大小为costSize

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /