746. 使用最小花费爬楼梯 (Swift版本)

题目

给你一个整数数组 cost,其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。

你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。

请你计算并返回达到楼梯顶部的最低花费。

限制条件

  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999

Show me the code

swift 复制代码
class Solution {
    var cacheMap = [Int: Int]()
    func minCostClimbingStairs(_ cost: [Int]) -> Int {
        return minCostClimbingStairs(cost, cost.count)
    }

    func minCostClimbingStairs(_ cost: [Int], _ prefixCount: Int) -> Int {
        if let cache = cacheMap[prefixCount] {
            return cache
        }
        if prefixCount == 2 {
            return min(cost[0], cost[1])
        }
        if prefixCount == 3 {
            return min(cost[0] + cost[2], cost[1])
        }
        let ret = min(minCostClimbingStairs(cost, prefixCount - 1) + cost[prefixCount - 1], minCostClimbingStairs(cost, prefixCount - 2) + cost[prefixCount - 2])
        cacheMap[prefixCount] = ret
        return ret
    }
}

执行用时&内存分布

相关推荐
Haha_bj1 天前
Swift ——详解Any、AnyObject、 AnyClass
ios·swift
Haha_bj1 天前
Swift 中的async和await
ios·swift
2501_915106321 天前
iOS 抓不到包怎么办?从 HTTPS 代理排查到 TCP 数据流捕获的全链路解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
游戏开发爱好者81 天前
APP上架苹果应用商店经验教训与注意事项
android·ios·小程序·https·uni-app·iphone·webview
张飞签名上架1 天前
苹果TF签名:革新应用分发的解决方案
人工智能·安全·ios·苹果签名·企业签名·苹果超级签名
WXG10111 天前
【Flask-7】前后端数据交互
python·ios·flask
笑尘pyrotechnic1 天前
手势识别器内容
ios·objective-c
他们都不看好你,偏偏你最不争气1 天前
【iOS】SDWebImage解析
macos·ios·objective-c·cocoa·sdwebimage
Digitally1 天前
如何从iPhone切换到Android
android·ios·iphone
2501_916007471 天前
苹果应用商店上架的系统逻辑,从产品开发到使用 开心上架 上架IPA 交付审核流程
android·ios·小程序·https·uni-app·iphone·webview