# LWC 63：746. Min Cost Climbing Stairs

### Problem:

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20] Output: 15 Explanation: Cheapest is start on cost1, pay that cost and go to the top.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] Output: 6 Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

Note:

cost will have a length in the range [2, 1000].

Every cost[i] will be an integer in the range [0, 999].

Java版本：

    public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
int[] dp = new int[n + 16];
for (int i = 2; i <= n; ++i) {
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}
return dp[n];
}

Python版本：

    def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
n = len(cost)
dp = [0] * (n + 1)
for i in xrange(2, n + 1):
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])

return dp[n]

98 篇文章33 人订阅

0 条评论

## 相关文章

### LWC 58：724. Find Pivot Index

LWC 58：724. Find Pivot Index 传送门：724. Find Pivot Index Problem: Given an array ...

1798

### Educational Codeforces Round 44 (Rated for Div. 2)A. Chess Placing

You are given a chessboard of size 1 × n. It is guaranteed that n is even. The c...

842

### Leetcode 55 Jump Game

Given an array of non-negative integers, you are initially positioned at the fi...

17210

321

3034

### Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did yo

C. Did you mean... time limit per test：1 second memory limit per test：256 megaby...

2625

### 1008 选数 2002年NOIP全国联赛普及组

1008 选数 2002年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果 ...

2687

541

673

### 洛谷P3372 【模板】线段树 1(树状数组)

$= sum_{i = 1}^x (x+1)d_i - \sum_{i = 1}^x id_i$

1405