[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 }

原文地址:https://www.cnblogs.com/strengthen/p/10260678.html

时间: 2024-10-31 04:45:39

[Swift]LeetCode322. 零钱兑换 | Coin Change的相关文章

动态规划LeetCode322零钱兑换

题目描述: 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = [1, 2, 5], amount = 11输出: 3 解释: 11 = 5 + 5 + 1示例 2: 输入: coins = [2], amount = 3输出: -1说明:你可以认为每种硬币的数量是无限的. 思路: 利用贪心算法可以吗? 算法思路: 1 class Solution

dp背包问题/01背包,完全背包,多重背包,/coin change算法求花硬币的种类数

一步一步循序渐进. Coin Change 具体思想:给你 N元,然后你有几种零钱S={S1,S2...,Sm} (每种零钱数量不限). 问:凑成N有多少种组合方式  即N=x1 * S1+x2*S2+...+xk*Sk (xk>=0,k=1,2..m) 设有f(x)中组合方式 有两种解答(自底向上回溯): 1.不用第m种货币   f(N,m-1) 2.用第m种货币 f(N-Sm,m) 总的组合方式为f(N,m)=f(N,m-1)+f(N-Sm,m) anything is nonsense,s

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accepted: 15289 Total Submissions: 62250 Difficulty: Medium You are given coins of different denominations and a total amount of money amount. Write a func

leetcode 322 零钱兑换

目录 1. 题目 2. 解法 2.1. 暴力穷举 2.2. 动态规划-1 自下而上 2.3. 动态规划-2 自上而下 3. 代码及测试结果 3.1. 测试代码: 3.2. 结果: 1. 题目 零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = [1, 2, 5], amount = 11 输出: 3 解释: 11 = 5 + 5 + 1

[LeetCode][JavaScript]Coin Change

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 o

Leetcode OJ --- 322. 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,

HDU 2069 Coin Change

Coin Change Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 206964-bit integer IO format: %I64d      Java class name: Main Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We

HDU 2069 Coin Change 母函数求解

HDU 2069 Coin Change 母函数求解 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2069 这题比普通的母函数求解题目复杂一点,多了组成个数的限制, 要求组合个数不能超过 100 个硬币,所以将 c1, c2 定义为二维数组, c1[i][j] 表示 c1[结果][组成该结果的个数] ,然后多了一层遍历个数的循环. // 母函数求解 #include <bits/stdc++.h> using namespace std; cons

SICP 习题 (2.19) 解题总结:重写零钱兑换程序

SICP 习题2.19 要求我们重新设计1.2.2节的零钱兑换程序,要求我们可以轻易改变程序里用的兑换币种. 我们先看看1.2.2节的零钱兑换程序,代码是这样的: (define (RMB-Change amount) (format #t "Changing ~S~%" amount) (cond ((= amount 0) 0) ((< amount 0) 0) (else (RMB-Change-Recursive amount 1 '() )))) (define (RM