comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
简单 |
|
给你一个整数数组 cost
,其中 cost[i]
是从楼梯第 i
个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。
你可以选择从下标为 0
或下标为 1
的台阶开始爬楼梯。
请你计算并返回达到楼梯顶部的最低花费。
示例 1:
输入:cost = [10,15,20] 输出:15 解释:你将从下标为 1 的台阶开始。 - 支付 15 ,向上爬两个台阶,到达楼梯顶部。 总花费为 15 。
示例 2:
输入:cost = [1,100,1,1,1,100,1,1,100,1] 输出:6 解释:你将从下标为 0 的台阶开始。 - 支付 1 ,向上爬两个台阶,到达下标为 2 的台阶。 - 支付 1 ,向上爬两个台阶,到达下标为 4 的台阶。 - 支付 1 ,向上爬两个台阶,到达下标为 6 的台阶。 - 支付 1 ,向上爬一个台阶,到达下标为 7 的台阶。 - 支付 1 ,向上爬两个台阶,到达下标为 9 的台阶。 - 支付 1 ,向上爬一个台阶,到达楼梯顶部。 总花费为 6 。
提示:
2 <= cost.length <= 1000
0 <= cost[i] <= 999
我们定义
当
最终的答案即为
时间复杂度 cost
的长度。
我们注意到,状态转移方程中的
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
f = [0] * (n + 1)
for i in range(2, n + 1):
f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1])
return f[n]
class Solution {
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
int[] f = new int[n + 1];
for (int i = 2; i <= n; ++i) {
f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
}
return f[n];
}
}
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
vector<int> f(n + 1);
for (int i = 2; i <= n; ++i) {
f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
}
return f[n];
}
};
func minCostClimbingStairs(cost []int) int {
n := len(cost)
f := make([]int, n+1)
for i := 2; i <= n; i++ {
f[i] = min(f[i-1]+cost[i-1], f[i-2]+cost[i-2])
}
return f[n]
}
function minCostClimbingStairs(cost: number[]): number {
const n = cost.length;
const f: number[] = Array(n + 1).fill(0);
for (let i = 2; i <= n; ++i) {
f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]);
}
return f[n];
}
impl Solution {
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 {
let n = cost.len();
let mut f = vec![0; n + 1];
for i in 2..=n {
f[i] = std::cmp::min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
}
f[n]
}
}
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
f = g = 0
for i in range(2, len(cost) + 1):
f, g = g, min(f + cost[i - 2], g + cost[i - 1])
return g
class Solution {
public int minCostClimbingStairs(int[] cost) {
int f = 0, g = 0;
for (int i = 2; i <= cost.length; ++i) {
int gg = Math.min(f + cost[i - 2], g + cost[i - 1]);
f = g;
g = gg;
}
return g;
}
}
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int f = 0, g = 0;
for (int i = 2; i <= cost.size(); ++i) {
int gg = min(f + cost[i - 2], g + cost[i - 1]);
f = g;
g = gg;
}
return g;
}
};
func minCostClimbingStairs(cost []int) int {
var f, g int
for i := 2; i <= n; i++ {
f, g = g, min(f+cost[i-2], g+cost[i-1])
}
return g
}
function minCostClimbingStairs(cost: number[]): number {
let a = 0,
b = 0;
for (let i = 1; i < cost.length; ++i) {
[a, b] = [b, Math.min(a + cost[i - 1], b + cost[i])];
}
return b;
}
impl Solution {
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 {
let (mut f, mut g) = (0, 0);
for i in 2..=cost.len() {
let gg = std::cmp::min(f + cost[i - 2], g + cost[i - 1]);
f = g;
g = gg;
}
g
}
}