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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-wnggoqxu-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).git

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

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:app

  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開始)。spa

每當你爬上一個階梯你都要花費對應的體力花費值,而後你能夠選擇繼續爬一個階梯或者爬兩個階梯。code

您須要找到達到樓層頂部的最低花費。在開始時,你能夠選擇從索引爲 0 或 1 的元素做爲初始階梯。htm

示例 1:blog

輸入: 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 }
相關文章
相關標籤/搜索