[Swift]LeetCode746. 使用最小花费爬楼梯 | Min Cost Climbing Stairs

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 cost[1], 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:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999].


数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。

每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。

您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。

示例 1:

输入: cost = [10, 15, 20]
输出: 15
解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15。

示例 2:

输入: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
输出: 6
解释: 最低花费方式是从cost[0]开始,逐个经过那些1,跳过cost[3],一共花费6。

注意:

  1. cost 的长度将会在 [2, 1000]
  2. 每一个 cost[i] 将会是一个Integer类型,范围为 [0, 999]


Runtime: 32 ms

Memory Usage: 18.9 MB

 1 class Solution {
 2     func minCostClimbingStairs(_ cost: [Int]) -> Int {
 3         var f: [Int] = []
 4         f.append(cost[0])
 5         for i in 1...(cost.count + 1) {
 6             var one = f[i - 1]
 7             var two = 0
 8             var now = 0
 9             if i - 2 < 0 {
10                 two = 0
11             } else {
12                 two = f[i - 2]
13             }
14             if i >= cost.count {
15                 now = 0
16             } else {
17                 now = cost[i]
18             }
19             f.append(min(one + now, two + now))
20         }
21         return min(f[cost.count], f[cost.count + 1])
22     }
23 }


32ms

 1 class Solution {
 2     func minCostClimbingStairs(_ cost: [Int]) -> Int {
 3         if cost.count == 1 {
 4             return cost[0]
 5         }
 6         if cost.count == 2 {
 7             return min(cost[0], cost[1])
 8         }
 9
10         var result = Array(repeating: 0, count: cost.count+1)
11         result[0] = 0
12         result[1] = 0
13         for i in 2...cost.count {
14             result[i] = min(result[i-2] + cost[i-2], result[i-1] + cost[i-1])
15         }
16         return result[cost.count]
17     }
18 }


36ms

 1 class Solution {
 2
 3     func minCostClimbingStairs(_ cost: [Int]) -> Int {
 4         var result1 = cost[0]
 5         var result2 = cost[1]
 6
 7         for i in 2 ..< cost.count {
 8             var base = min(result1, result2)
 9             result1 = result2
10             result2 = cost[i] + base
11         }
12
13         return min(result1, result2)
14     }
15 }


40ms

 1 class Solution {
 2     func minCostClimbingStairs(_ cost: [Int]) -> Int {
 3         var step1 = 0, step2 = 0
 4         for i in cost.indices.dropFirst(2) {
 5             let step3 = min(step1 + cost[i - 2], step2 + cost[i - 1])
 6             step1 = step2
 7             step2 = step3
 8         }
 9         return min(step1 + cost.dropLast().last!, step2 + cost.last!)
10     }
11 }


48ms

 1 class Solution {
 2     func minCostClimbingStairs(_ cost: [Int]) -> Int {
 3         var minCost = cost
 4         for i in 2 ..< cost.count{
 5             minCost[i] += min(minCost[i-1], minCost[i-2])
 6         }
 7
 8         return min(minCost[cost.count-1], minCost[cost.count-2])
 9     }
10 }


64ms

 1 class Solution {
 2     func minCostClimbingStairs(_ cost: [Int]) -> Int {
 3         let n = cost.count
 4         var value1 = cost[0]
 5         var value2 = cost[1]
 6         for i in 2..<n {
 7             (value2, value1) = (min(value2, value1) + cost[i], value2)
 8         }
 9         return min(value1, value2)
10     }
11 }

原文地址:https://www.cnblogs.com/strengthen/p/10525485.html

时间: 2024-11-09 10:08:04

[Swift]LeetCode746. 使用最小花费爬楼梯 | Min Cost Climbing Stairs的相关文章

Min Cost Climbing Stairs [746]

Min Cost Climbing Stairs [746] 题目描述 简单来说就是:要跳过当前楼梯需要花费当前楼梯所代表的价值cost[i], 花费cost[i]之后,可以选择跳一阶或者两阶楼梯,以最小的代价达到楼层,也就是跨过所有楼梯 问题解决 穷举法 从第一阶楼梯开始,遍历所有可能的情况,然后选择代价最小的.复杂度会比较高,时间复杂度O(2^n),不太适合 动态规划 逆向解决:从后往前倒退,跳过当前阶楼梯代价最小的情况下需要考虑其面楼梯代价最小的情况,而且每次只能跳一阶或者两阶,也就是说跳

[LC难题必须要解决系列][之][DP] Min Cost Climbing Stairs

Min Cost Climbing Stairs https://leetcode.com/problems/min-cost-climbing-stairs/ 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 min

746. 使用最小花费爬楼梯

题目描述 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 costi. 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需要找到达到楼层顶部的最低花费.在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯. 示例 1: 输入: cost = [10, 15, 20] 输出: 15 解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15. 示例 2: 输入: cost = [1, 100, 1, 1,

力扣 746. 使用最小花费爬楼梯

数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 costi. 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需要找到达到楼层顶部的最低花费.在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯. 示例 1: 输入: cost = [10, 15, 20] 输出: 15 解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15. 示例 2: 输入: cost = [1, 100, 1, 1, 1, 1

746使用最小花费爬楼梯

参考答案,特别鸣谢:https://leetcode-cn.com/problems/min-cost-climbing-stairs/solution/scala-dong-tai-gui-hua-di-gui-by-zx233/ 0. 题目描述 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需要找到达到楼层顶部的最低花费.在开始时,你可以选择

Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [10, 15, 20] Output: 15 Explanation: 从第一阶出发,一次走两步 Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] Output: 6 Explanation: 从地面出发,走两步,走两步,走两步,走一步,走两步,走一

746. Min Cost Climbing Stairs 最不费力的加权爬楼梯

[抄题]: 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

[LeetCode] Min Cost Climbing Stairs

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 w

[LC] 746. Min Cost Climbing Stairs

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 w