mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0322.零钱兑换.md
This commit is contained in:
@ -216,37 +216,75 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
先遍历物品 后遍历背包
|
||||
```python
|
||||
class Solution:
|
||||
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||
'''版本一'''
|
||||
# 初始化
|
||||
dp = [float("inf")]*(amount + 1)
|
||||
dp[0] = 0
|
||||
# 遍历物品
|
||||
for coin in coins:
|
||||
# 遍历背包
|
||||
for j in range(coin, amount + 1):
|
||||
dp[j] = min(dp[j], dp[j - coin] + 1)
|
||||
return dp[amount] if dp[amount] != float("inf") else -1
|
||||
dp = [float('inf')] * (amount + 1) # 创建动态规划数组,初始值为正无穷大
|
||||
dp[0] = 0 # 初始化背包容量为0时的最小硬币数量为0
|
||||
|
||||
for coin in coins: # 遍历硬币列表,相当于遍历物品
|
||||
for i in range(coin, amount + 1): # 遍历背包容量
|
||||
if dp[i - coin] != float('inf'): # 如果dp[i - coin]不是初始值,则进行状态转移
|
||||
dp[i] = min(dp[i - coin] + 1, dp[i]) # 更新最小硬币数量
|
||||
|
||||
if dp[amount] == float('inf'): # 如果最终背包容量的最小硬币数量仍为正无穷大,表示无解
|
||||
return -1
|
||||
return dp[amount] # 返回背包容量为amount时的最小硬币数量
|
||||
|
||||
def coinChange1(self, coins: List[int], amount: int) -> int:
|
||||
'''版本二'''
|
||||
# 初始化
|
||||
dp = [float("inf")]*(amount + 1)
|
||||
dp[0] = 0
|
||||
# 遍历物品
|
||||
for j in range(1, amount + 1):
|
||||
# 遍历背包
|
||||
for coin in coins:
|
||||
if j >= coin:
|
||||
dp[j] = min(dp[j], dp[j - coin] + 1)
|
||||
return dp[amount] if dp[amount] != float("inf") else -1
|
||||
```
|
||||
|
||||
先遍历背包 后遍历物品
|
||||
```python
|
||||
class Solution:
|
||||
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||
dp = [float('inf')] * (amount + 1) # 创建动态规划数组,初始值为正无穷大
|
||||
dp[0] = 0 # 初始化背包容量为0时的最小硬币数量为0
|
||||
|
||||
for i in range(1, amount + 1): # 遍历背包容量
|
||||
for j in range(len(coins)): # 遍历硬币列表,相当于遍历物品
|
||||
if i - coins[j] >= 0 and dp[i - coins[j]] != float('inf'): # 如果dp[i - coins[j]]不是初始值,则进行状态转移
|
||||
dp[i] = min(dp[i - coins[j]] + 1, dp[i]) # 更新最小硬币数量
|
||||
|
||||
if dp[amount] == float('inf'): # 如果最终背包容量的最小硬币数量仍为正无穷大,表示无解
|
||||
return -1
|
||||
return dp[amount] # 返回背包容量为amount时的最小硬币数量
|
||||
|
||||
```
|
||||
先遍历物品 后遍历背包(优化版)
|
||||
```python
|
||||
class Solution:
|
||||
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||
dp = [float('inf')] * (amount + 1)
|
||||
dp[0] = 0
|
||||
|
||||
for coin in coins:
|
||||
for i in range(coin, amount + 1):
|
||||
# 更新凑成金额 i 所需的最少硬币数量
|
||||
dp[i] = min(dp[i], dp[i - coin] + 1)
|
||||
|
||||
return dp[amount] if dp[amount] != float('inf') else -1
|
||||
|
||||
|
||||
```
|
||||
先遍历背包 后遍历物品(优化版)
|
||||
```python
|
||||
class Solution:
|
||||
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||
dp = [float('inf')] * (amount + 1)
|
||||
dp[0] = 0
|
||||
|
||||
for i in range(1, amount + 1): # 遍历背包容量
|
||||
for coin in coins: # 遍历物品
|
||||
if i - coin >= 0:
|
||||
# 更新凑成金额 i 所需的最少硬币数量
|
||||
dp[i] = min(dp[i], dp[i - coin] + 1)
|
||||
|
||||
return dp[amount] if dp[amount] != float('inf') else -1
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
```go
|
||||
|
Reference in New Issue
Block a user