小工具知识网

当前位置: 首页 > 行业知识 > 746. 使用最小花费爬楼梯 (Swift版本)

行业知识

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

2026-11-12 08:36:43 来源:互联网转载

题目

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

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

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

限制条件

  • 2 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 }}

上一篇:魅力炸裂!揭秘无与伦比的酷炫魅力

下一篇:服务器端口怎么查看腾讯云?