微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift]LeetCode322. 零钱兑换 | Coin Change

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] = 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 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐