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:
input: coins =,amount = Output: Explanation: 11 = 5 + 5 + 1[1,2,5]113
Example 2:
input: coins =,amount = Output: -1[2]3
Note:
You may assume that you have an infinite number of each kind of coin.
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1
。
示例 1:
输入: coins =,amount = 输出: 解释: 11 = 5 + 5 + 1[1,5]113
示例 2:
输入: coins =,amount = 输出: -1[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] = 011 12 for i in 0..<amount {13 if nums[i] == -1 {14 continue15 }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] + 120 }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 continue12 } else {13 dp[i] = min(dp[i],dp[i - coin] + 1)14 }15 }16 }17 }18 if dp[amount] == Int.max {19 return -120 } 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 -114 }15 16 guard rem != 0 else {17 return 018 }19 20 guard count[rem - 1] == 0 else {21 return count[rem - 1]22 }23 24 var min = Int.max25 for coin in coins {26 let res = coinChange(coins,rem - coin,&count)27 if res >= 0 && res < min {28 min = res + 129 }30 }31 count[rem - 1] = min == Int.max ? -1 : min32 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.max16 }17 18 lookupRes[0] = 019 for amountIndex in 1...amount {20 for var i in 0..<coins.count21 { 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 -135 }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] = -110 }11 for coin in coins {12 nums[coin] = 113 }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 + 121 } else {22 nums[i] = min(target + 1,nums[i] ?? 0)23 }24 }25 }26 }27 }28 return nums[amount] ?? 029 }30 }总结
以上是内存溢出为你收集整理的[Swift]LeetCode322. 零钱兑换 | Coin Change全部内容,希望文章能够帮你解决[Swift]LeetCode322. 零钱兑换 | Coin Change所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)