1. 程式人生 > >[Swift]LeetCode322. 零錢兌換 | Coin Change

[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 = [1, 2, 5]
, amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Note:
You may assume that you have an infinite number of each kind of coin.


給定不同面額的硬幣 coins 和一個總金額 amount。編寫一個函式來計算可以湊成總金額所需的最少的硬幣個數。如果沒有任何一種硬幣組合能組成總金額,返回 -1

示例 1:

輸入: coins = [1, 2, 5]
, amount = 11 輸出: 3 解釋: 11 = 5 + 5 + 1

示例 2:

輸入: coins = [2], amount = 3
輸出: -1

說明:
你可以認為每種硬幣的數量是無限的。


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 }