当前位置:网站首页>leetcode: 322.零钱兑换
leetcode: 322.零钱兑换
2022-08-08 03:38:00 【uncle_ll】
322.零钱兑换
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/coin-change/
给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额。
计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额,返回 -1 。
你可以认为每种硬币的数量是无限的。
示例 1:
输入:coins = [1, 2, 5], amount = 11
输出:3
解释:11 = 5 + 5 + 1
示例 2:
输入:coins = [2], amount = 3
输出:-1
示例 3:
输入:coins = [1], amount = 0
输出:0
提示:
- 1 <= coins.length <= 12
- 1 <= coins[i] <= 2 31 − 1 2^{31} - 1 231−1
- 0 <= amount <= 1 0 4 10^4 104
解法
- 贪心算法: 这题不能只用贪心算法去做,每次找最大的,然后往后找次小的,对于一些给定的coins而言是没法得到结果的,这里可以从后往前思考:
- F(S) = F(S−C) + 1, F(S)就是组成金额S所需的最少硬币数量,C就是可选的硬币额度,我们这里需要找到最小的情况,因此,需要对每个硬币进行一次遍历,然后比较,这里需要使用dfs递归的思想,递归结束条件,F(0) = 0, F(S<0) = -1;
- 递归会比较慢,有一些重复计算,这里加入缓存或者使用列表进行存储加快速度
代码实现
贪心算法
python实现
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount <= 0:
return 0
@functools.lru_cache(amount)
def dfs(remian_amount):
if remian_amount < 0:
return -1
if remian_amount == 0:
return 0
min_val = float('inf')
for coin in coins:
res = dfs(remian_amount-coin)
if res >= 0 and res + 1 < min_val:
min_val = res + 1
return min_val if min_val < float('inf') else -1
res = dfs(amount)
print(dfs.cache_info())
return res
c++实现
class Solution {
vector<int>count;
int dp(vector<int>& coins, int rem) {
if (rem < 0) return -1;
if (rem == 0) return 0;
if (count[rem - 1] != 0) return count[rem - 1];
int Min = INT_MAX;
for (int coin:coins) {
int res = dp(coins, rem - coin);
if (res >= 0 && res < Min) {
Min = res + 1;
}
}
count[rem - 1] = Min == INT_MAX ? -1 : Min;
return count[rem - 1];
}
public:
int coinChange(vector<int>& coins, int amount) {
if (amount < 1) return 0;
count.resize(amount);
return dp(coins, amount);
}
};
复杂度分析
- 时间复杂度: O ( S N ) O(SN) O(SN), S是金额,n是面额数
- 空间复杂度: O ( S ) O(S) O(S) 需要存储一个长为S的数组来存储计算出来的答案
其实最好的解决办法是使用动态规划,这里不做过多的详述。
边栏推荐
- Data labeling platform doccano----Introduction, installation, use, pit record
- PTA Exercise 1.8 Binary Search
- 监控工具Prometheus及项目总结,220805,,
- 程序中的负数存储及类型转换
- 任务调度框架 Quartz 一文读懂
- After nibbling on this Ali manual, the new year will enter Ali from seven sides
- JVM调优的策略
- STFW3N150管脚功能 数据表(PDF)引脚图
- The type of block in the database buffer cache
- Kubernetes Technology and Architecture (8)
猜你喜欢

Where to open a futures account with low fees and high returns

KDD'22 | CausalMTA: Unbiased Advertising Multi-Touch Attribution Technology Based on Causal Inference

Online futures account opening is becoming more and more popular

使用z-file和七牛云对象存储构建个人网盘

小程序优化实践

vulnhub-DC-3靶机渗透记录

从精装到智装,下一波浪潮浮现,听听智能家居的大咖们怎么说?

New retail project and offline warehouse core interview,, 220807,,

Nanny level tutorial!Golang microservices simple architecture in practice

数据在内存如何分布的?
随机推荐
find prime problem
121. The Best Time to Buy and Sell Stock, the Best opportunity to Buy and Sell stocks
新零售项目及离线数仓核心面试,,220807,,
1.9 and orderly array insert PTA problem sets
egg-session stores data to redis
KDD'22 | CausalMTA: Unbiased Advertising Multi-Touch Attribution Technology Based on Causal Inference
文本生成介绍
农产品直播带货持续升温,经济日报:冲流量勿忘质量
stack push and pop sequence
测试岗电话面试——必问题型
任务调度框架 Quartz 一文读懂
VSCode opens some records of C (embedded) projects
egg-validate-custom validation method error language (error Chinese prompt)
从精装到智装,下一波浪潮浮现,听听智能家居的大咖们怎么说?
CGAN theory explanation and code implementation
Embedded Interview Questions
高效记忆法
剑指Offer 18.删除链表的节点
开发如何尽可能的避免BUG
egg-session 将数据存储到redis