问题-你会得到不同面额的硬币和总金额。写一个函数来计算你需要的最少数量的硬币来组成这个数量。那里有无限的硬币供应。
我的方法——我遵循了自上而下的方法,但是使用map stl进行记忆,我得到了TLE。请帮助找出误差和估计时间复杂度。
这是我的密码-
// for example coins v = {1,2} and W = 2 then possible solutions are -
// (2) or (1, 1), therefore minimum number of coins required is 1.
// Below is the function calculating the minimum number of coins required for change
int minCoinChange(vector<int> &v, int start, int W, unordered_map<string, int> &lookup){
// v denoting the vector conataining coins with given denomination
// start denoting the start index i.e. 0(initially)
// W denoting the required change
// lookup is map stl for storing values.
// base cases
if(W<0){
return INT_MAX;
}
if(W == 0){
return 0;
}
if(start >= v.size()){
return INT_MAX;
}
// for memoization creating the "key"
string key = to_string(start) + '|' + to_string(W);
// if the key is not found in map then go inside if
if(lookup.find(key) == lookup.end()){
int excl = minCoinChange(v, start+1, W, lookup); // if element is excluded
int incl = 1 + minCoinChange(v, start, W - v[start], lookup); if element is included
lookup[key] = min(incl, excl); // calculating minimum number of coins required and storing in map
}
// if key is already there then return value stored in map
return lookup[key];
}
首先,unordered_map
有一个最坏情况O(n)
查找时间。有一些方法可以优化unordered_map
性能,比如用map.reserve(n)
保留桶的数量,而n
是您期望在地图中拥有的唯一项的数量。
您可以使用map
,它具有最坏情况下的O(log(n))
查找时间,但是由于该算法的时间复杂度是O(n*W)
您的n
和W
将足够小,可以放入数组中,然后您将拥有O(1)
查找。
下面是对函数的修改,改为使用数组查找:
int minCoinChange(vector<int> &v, int start, int W, vector<vector<int>> &lookup){
// v denoting the vector conataining coins with given denomination
// start denoting the start index i.e. 0(initially)
// W denoting the required change
// lookup is 2d vector for storing already computed values.
// base cases
if (W<0) {
return 1e9;
}
if (W == 0) {
return 0;
}
if (start >= v.size()) {
return 1e9;
}
// if this state wasn't computed before
if (lookup[start][W] == -1) {
int excl = minCoinChange(v, start+1, W, lookup); // if element is excluded
int incl = 1 + minCoinChange(v, start, W - v[start], lookup); // if element is included
lookup[start][W] = min(incl, excl); // calculating minimum number of coins required and storing in map
}
// return the saved value
return lookup[start][W];
}
注:
对于基本情况,不要使用INT\u MAX
,如果向其添加1
,则会溢出,请使用类似10^9
的内容。
我编写的代码使用动态规划解决了基本的硬币兑换问题,并给出了进行兑换所需的最小硬币数量。但是我想把每个硬币的数量存储在最小的数字里。 我试图做的是初始化数组,就像散列一样,只要找到,它就会增加的数量,即。但这并不是我想要的方式,因为它每次发现对应于时都会添加硬币。因此,在最终答案中,数字不是硬币的最终计数。 代码如下:
但是我们不能这样做吗:(是给定的可用硬币的排序集,和是它的下标,是给定的最高价值硬币) 我写的东西有什么问题吗?虽然解决方案不是动态的,但不是更有效吗?
在硬币系统C={c1,c2,…ck}中,应改变给定的数量x,以使每个硬币ci具有给定的重量wi。我们想计算可能变化的总重量。两种变化是不同的,如果他们包含在不同的顺序相同的硬币。 如何给出上述问题的动态规划递归?我知道最小硬币兑换问题的递归(即C(x)=min{C(x-C)1 for x
我一直在使用动态规划研究硬币兑换问题。我尝试制作一个数组fin[],其中包含索引所需的最小硬币数,然后打印它。我已经写了一个代码,我认为应该给出正确的输出,但我不明白为什么它没有给出准确的答案。例如:对于输入:4 3 1 2 3(4是要更改的金额,3是可用硬币的类型,1 2 3是硬币值列表)输出应该是:0 1 1 2(因为我们有1,2,3作为可用硬币,需要0个硬币更改0,1个硬币更改1,1个硬币更
http://uva.onlinejudge.org/external/6/674.html我正在努力解决这个问题。不过,请注意,这不是最小硬币更换问题,它要求我使用50, 25, 15, 10, 5和1美分硬币制作N美分的不同方法。它相当简单,所以我做了这个函数: 同样简单的是添加动态编程和备忘录: 然而,这些都不够快-我需要自下而上的动态规划,但我在编码它时遇到困难,即使在算法学家的帮助下-h
我很难理解这个问题背后的逻辑,这是经典的动态规划问题 我知道递归是如何工作的,比如拿不拿mth硬币。但我不明白这两个州之间的关系。 例如 这个问题可能很愚蠢,但我还是想知道,这样我才能更好地理解。谢谢