首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
兑换零钱(一)
[编程题]兑换零钱(一)
热度指数:93532
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定数组arr,arr中所有的值都为正整数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个aim,代表要找的钱数,求组成aim的最少货币数。
如果无解,请返回-1.
数据范围:数组大小满足
, 数组中每个数字都满足
,
要求:时间复杂度
,空间复杂度
。
示例1
输入
[5,2,3],20
输出
4
示例2
输入
[5,2,3],0
输出
0
示例3
输入
[3,5],2
输出
-1
备注:
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(1155)
分享
提交结果有问题?
119个回答
131篇题解
开通博客
牛客题解官
发表于 2022-04-22 12:45:29
精华题解
题目主要信息: 给定数组arr,arr中所有的值都为正整数且不重复 arr中每个值代表一种面值的货币,每种面值的货币可以使用任意 组成aim的最少货币数 如果无解,请返回-1 举一反三: 本题属于背包问题的一种简化版本,学习完本题的思路帮助你解决相似的背包问题。 方法一:动态规划(推荐使用) 知
展开全文
小洋芋热爱NLP
发表于 2021-02-01 23:01:40
- 1、题目描述: - 2、题目链接: https://www.nowcoder.com/practice/3911a20b3f8743058214ceaa099eeb45?tpId=196&&tqId=37128&rp=1&ru=/activity/oj&q
展开全文
牛客82035003号
发表于 2022-05-03 21:02:07
就拿给定的例子来说,有5,2,3,三种面值的钱,现在拿出一张20元的,怎么兑换才能使钱的张数最少。 首先,我们知道可兑换的钱的最大张数是20,也就是面值是1的时候,其他可兑换的情况张数肯定小于20,那么可兑换张数<=20才是可考虑的范围,且要求最小值。 设置的计数数组的
展开全文
超级码力233
发表于 2020-11-26 16:47:31
换钱的最少货币数 题目链接 Solution 问n种货币凑成一个面额所需要的最少货币数。动态规划。 表示凑成面额i的需要的最少货币数。然后枚举每个面额的货币,更新dp数组即可。 Code class Solution { public: int minMoney(vector<int
展开全文
牛客877483763号
发表于 2021-12-20 10:42:43
兑换零钱(一) 描述 给定数组arr,arr中所有的值都为正整数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个aim,代表要找的钱数,求组成aim的最少货币数。 如果无解,请返回-1. 数据范围:数组大小满足 0≤n≤10000 , 数组中每个数字都满足 0<va
展开全文
牛客516598323号
发表于 2020-09-29 10:27:59
有点难理解,其实还是递归,https://www.jianshu.com/p/b9d7ff91684e i: 代表可以使用的货币种类为 arr[0..i]j: 代表需要兑换的面值数,其取值范围为[0..aim],因此实现时,二维数组的列数应该为aim + 1对于dp[i][j],更新的依据有两个值
展开全文
小陆要懂云
发表于 2021-07-17 16:21:44
C++ 动态规划问题 要点: dp[j] = min(dp[j], dp[j - arr[i]] + 1); dp[0] = 0; int dp[aim + 1]; dp[0] = 0; for (int i = 1; i <= aim; ++i) dp[i] = IN
展开全文
我和我
发表于 2021-11-10 22:44:31
public class Solution { /** * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */
展开全文
deamn
发表于 2022-05-04 12:52:15
简单的动态规划思想 class Solution: def minMoney(self , coins: List[int], amount: int) -> int: dp=[float('inf')]*(amount+1) dp[0]=0
展开全文
youxiwang
发表于 2022-02-07 10:18:48
完全背包变形 dp[i][v]: 用前i个硬币组合出价值v所需的最少硬币数 dp[0][0]=1, dp[0][v]=0 for v>0. dp[i][v] = Min { (a) dp[i-1][v], // 不用i硬币 (b) dp[i][v-arr[i]] // 用i
展开全文
馒头2020
发表于 2021-01-22 11:00:31
题目描述 给定数组arr,arr中所有的值都为正整数且不重复。每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个aim,代表要找的钱数,求组成aim的最少货币数。如果无解,请返回-1.【要求】时间复杂度O(n × aim),空间复杂度On。 示例1 输入 [5,2,3],20 返回值
展开全文
问题信息
动态规划
难度:
119条回答
1155收藏
6470浏览
热门推荐
通过挑战的用户
查看代码
牛客89418...
2022-10-26 20:19:52
牛客74843...
2022-10-10 20:56:54
牛客80946...
2022-09-16 16:40:28
LeeZhiyu
2022-09-16 15:48:40
DragonN...
2022-09-16 15:01:37
相关试题
请画出在包含 14 个结点的二项堆...
高级算法
评论
(1)
如图 1 表示使用快表(页表)的虚...
编程基础
评论
(1)
对于我们来说,谁是好的顾客?
销售常识
评论
(1)
小红书用户在不同使用场景下,对内容...
需求分析
评论
(1)
订单表order_table全部记...
查找
数据库
数据分析
SQL
评论
(1)
兑换零钱(一)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ public int minMoney (int[] arr, int aim) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型vector the array * @param aim int整型 the target * @return int整型 */ int minMoney(vector
& arr, int aim) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 最少货币数 # @param arr int整型一维数组 the array # @param aim int整型 the target # @return int整型 # class Solution: def minMoney(self , arr , aim ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ public int minMoney (List
arr, int aim) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ function minMoney( arr , aim ) { // write code here } module.exports = { minMoney : minMoney };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 最少货币数 # @param arr int整型一维数组 the array # @param aim int整型 the target # @return int整型 # class Solution: def minMoney(self , arr: List[int], aim: int) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ func minMoney( arr []int , aim int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param arrLen int arr数组长度 * @param aim int整型 the target * @return int整型 */ int minMoney(int* arr, int arrLen, int aim ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 最少货币数 # @param arr int整型一维数组 the array # @param aim int整型 the target # @return int整型 # class Solution def minMoney(arr, aim) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ def minMoney(arr: Array[Int],aim: Int): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ fun minMoney(arr: IntArray,aim: Int): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ public int minMoney (int[] arr, int aim) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ export function minMoney(arr: number[], aim: number): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ func minMoney ( _ arr: [Int], _ aim: Int) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 最少货币数 * @param arr int整型一维数组 the array * @param aim int整型 the target * @return int整型 */ pub fn minMoney(&self, arr: Vec
, aim: i32) -> i32 { // write code here } }
[5,2,3],20
4
[5,2,3],0
0
[3,5],2
-1