# C#LeetCode刷题之#746-使用最小花费爬楼梯（ Min Cost Climbing Stairs）

cost 的长度将会在 [2, 1000]。

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.

Input: cost = [10, 15, 20]

Output: 15

Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

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].

```public class Program {

public static void Main(string[] args) {
int[] cost = null;

cost = new int[] { 10, 15, 20 };
var res = MinCostClimbingStairs(cost);
Console.WriteLine(res);

cost = new int[] { 1, 100, 1, 1, 1, 100, 1, 1, 100, 1 };
res = MinCostClimbingStairs2(cost);
Console.WriteLine(res);

}

private static int MinCostClimbingStairs(int[] cost) {
int length = cost.Length + 1;
int[] step = new int[length];
step[0] = step[1] = 0;
for(int i = 2; i < length; i++) {
//每次选小往上撸
step[i] = Math.Min(step[i - 2] + cost[i - 2], step[i - 1] + cost[i - 1]);
}
return step[length - 1];
}

private static int MinCostClimbingStairs2(int[] cost) {
//减少空间复杂度的解法，原理同上面的
int step1 = cost[0];
int step2 = cost[1];
int min = Math.Min(step1, step2);
for(int i = 2; i < cost.Length; i++) {
step2 = step1;
step1 = min + cost[i];
min = Math.Min(step1, step2);
}
return min;
}

}```

```15
6```