[Swift]LeetCode322. 零钱兑换 | Coin Change
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10260678.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1
.
Example 1:
[1, 2, 5]
11
3
Example 2:
[2]
3
Note:
You may assume that you have an infinite number of each kind of coin.
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1
。
示例 1:
[1, 2, 5]
11
3
示例 2:
[2]
3
说明:
你可以认为每种硬币的数量是无限的。
148ms
1 class Solution { 2 func coinChange(_ coins: [Int], _ amount: Int) -> Int { 3 if amount == 0 { 4 return 0 5 } 6 if amount < 0 { 7 return -1 8 } 9 var nums = [Int].init(repeating: -1, count: amount+1) 10 nums[0] = 0 11 12 for i in 0..<amount { 13 if nums[i] == -1 { 14 continue 15 } 16 for j in 0..<coins.count{ 17 let k = i + coins[j] 18 if k <= amount && (nums[k] == -1 || nums[k] > nums[i] + 1) { 19 nums[k] = nums[i] + 1 20 } 21 } 22 23 } 24 return nums[amount] 25 } 26 }
324ms
1 class Solution { 2 func coinChange(_ coins: [Int], _ amount: Int) -> Int { 3 guard amount > 0 else { 4 return 0 5 } 6 var dp = [0] 7 for i in 1...amount { 8 dp.append(amount + 1) 9 for j in 0..<coins.count { 10 if i >= coins[j] { 11 dp[i] = min(dp[i], (dp[i - coins[j]] + 1)) 12 } 13 } 14 } 15 return dp[amount] == (amount + 1) ? -1 : dp[amount] 16 } 17 }
408ms
1 class Solution { 2 func coinChange(_ coins: [Int], _ amount: Int) -> Int { 3 if amount <= 0 { return 0 } 4 var dp = [Int](repeating: Int.max, count: amount + 1) 5 dp[0] = 0 6 for i in 1 ... amount { 7 for j in 0 ..< coins.count { 8 let coin = coins[j] 9 if coin <= i { 10 if dp[i - coin] == Int.max { 11 continue 12 } else { 13 dp[i] = min(dp[i], dp[i - coin] + 1) 14 } 15 } 16 } 17 } 18 if dp[amount] == Int.max { 19 return -1 20 } else { 21 return dp[amount] 22 } 23 } 24 }
864ms
1 class Solution { 2 func coinChange(_ coins: [Int], _ amount: Int) -> Int { 3 if amount == 0 { 4 return 0 5 } 6 var changes = Array(repeating: amount+1, count: amount+1) 7 changes[0] = 0 8 9 for i in 1...amount{ 10 for c in coins { 11 if i - c >= 0 { 12 changes[i] = min(changes[i], changes[i-c] + 1) 13 } 14 } 15 } 16 17 return changes[amount] == amount + 1 ? -1 : changes[amount] 18 } 19 }
936ms
1 class Solution { 2 func coinChange(_ coins: [Int], _ amount: Int) -> Int { 3 guard amount > 0 else { 4 return 0 5 } 6 7 var count = Array(repeating: 0, count: amount) 8 return coinChange(coins, amount, &count) 9 } 10 11 func coinChange(_ coins: [Int], _ rem: Int, _ count: inout [Int]) -> Int { 12 guard rem >= 0 else { 13 return -1 14 } 15 16 guard rem != 0 else { 17 return 0 18 } 19 20 guard count[rem - 1] == 0 else { 21 return count[rem - 1] 22 } 23 24 var min = Int.max 25 for coin in coins { 26 let res = coinChange(coins, rem - coin, &count) 27 if res >= 0 && res < min { 28 min = res + 1 29 } 30 } 31 count[rem - 1] = min == Int.max ? -1 : min 32 return count[rem - 1] 33 } 34 }
1048ms
1 class Solution { 2 func coinChange(_ coins: [Int], _ amount: Int) -> Int { 3 var coinsArr = coins.sorted(by: { 4 $0 > $1 5 }) 6 7 if (amount == 0) { 8 return 0; 9 } 10 11 // Initialize result 12 var res = Int.max; 13 var lookupRes:[Int:Int] = [:] 14 for var i in 1...amount+1 { 15 lookupRes[i] = Int.max 16 } 17 18 lookupRes[0] = 0 19 for amountIndex in 1...amount { 20 for var i in 0..<coins.count 21 { 22 if (coins[i] <= amountIndex) 23 { 24 let newAmount = amountIndex - coins[i] 25 let sub_res = lookupRes[newAmount]! 26 if (sub_res != Int.max && sub_res + 1 < lookupRes[amountIndex]!) { 27 lookupRes[amountIndex] = sub_res + 1; 28 } 29 } 30 } 31 } 32 33 if( lookupRes[amount]! == Int.max) { 34 return -1 35 } 36 return lookupRes[amount]! 37 } 38 }
4164ms
1 class Solution { 2 func coinChange(_ coins: [Int], _ amount: Int) -> Int { 3 guard amount > 0 else { 4 return 0 5 } 6 7 var nums = [Int : Int]() 8 for i in 1...amount { 9 nums[i] = -1 10 } 11 for coin in coins { 12 nums[coin] = 1 13 } 14 15 for i in 1...amount { 16 if let cur = nums[i], cur == -1 { 17 for coin in coins { 18 if let target = nums[i - coin], target != -1 { 19 if nums[i] == -1 { 20 nums[i] = target + 1 21 } else { 22 nums[i] = min(target + 1, nums[i] ?? 0) 23 } 24 } 25 } 26 } 27 } 28 return nums[amount] ?? 0 29 } 30 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了