Algorithm Leetcode硬币更换超时
下面的输入获得超时,如果19->18或更少,它将通过Algorithm Leetcode硬币更换超时,algorithm,recursion,dynamic-programming,Algorithm,Recursion,Dynamic Programming,下面的输入获得超时,如果19->18或更少,它将通过 [1,2,5] 19 有人知道出了什么问题吗?如果我们使用额外的内存来解决问题,可能会更容易: const coinChange = function(coins, amount) { const inf = Math.pow(2, 31) const dp = [] dp[0] = 0 while (dp.length <= amount) { let curr = inf - 1
[1,2,5]
19
有人知道出了什么问题吗?如果我们使用额外的内存来解决问题,可能会更容易:
const coinChange = function(coins, amount) {
const inf = Math.pow(2, 31)
const dp = []
dp[0] = 0
while (dp.length <= amount) {
let curr = inf - 1
for (let index = 0; index < coins.length; index++) {
if (dp.length - coins[index] < 0) {
continue
}
curr = Math.min(curr, 1 + dp[dp.length - coins[index]])
}
dp.push(curr)
}
return dp[amount] == inf - 1 ? -1 : dp[amount]
};
使用辅助函数Java:
class Solution {
public int coinChange(int[] coins, int target) {
if (target < 1)
return 0;
return helper(coins, target, new int[target]);
}
private int helper(int[] coins, int rem, int[] count) {
if (rem < 0)
return -1;
if (rem == 0)
return 0;
if (count[rem - 1] != 0)
return count[rem - 1];
int min = Integer.MAX_VALUE;
for (int coin : coins) {
int res = helper(coins, rem - coin, count);
if (res >= 0 && res < min)
min = 1 + res;
}
count[rem - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
return count[rem - 1];
}
}
工具书类
有关更多详细信息,请参阅,您可以在其中找到大量解释良好的公认解决方案,包括低复杂度算法和渐近/分析。
代码至少有一个问题。若要查看,请更改此行:
h[index] = mi < h[index] ? mi : h[index];
我认为,另一个问题是,想要更新h[index]中记录的结果,但同时又想在更新之前返回其中存储的结果,两者之间存在差异
假设我们可以通过各种方式达到目标0,那么考虑一下,如果第一个递归分支以多个步骤的路径到达目标0,那么当前会发生什么情况。以下内容能够通过测试用例 修正了何时更新散列
// keep it infinity or 0 or positive
h[index] = (h[index] === undefined ? mi : mi < h[index] ? mi : h[index]);
对于不懂这种语言javascript?的人来说,不清楚在通过另一条路径(即另一个i索引)时,是否可以看到在一条路径中获得的h数组信息。如果没有,您需要一个全局h数组。如果您希望有人帮助您调试代码,请清楚地命名您的变量。你也可以在代码中添加更多关于你期望它做什么的注释。@Damien抱歉,更新了。@I1490㪞I1491; I1489; I1511; I1503;更新了。这如何回答OP的问题,出了什么问题?随着他们的实施。在网上找到现成的硬币兑换问题解决方案并不困难。当然,这不是我们来这里的目的。这怎么可能,h[index]=h[index]==undefined。。。是否按定义进行过评估?如果定义了h[index],我们不在cc函数的顶部返回它的值吗?在这一点上,它不是总是未定义的,所以我们可以将它设置为mi吗?
h[index] = mi < h[index] ? mi : h[index];
console.log(`Before: t: ${ t }, mi: ${ mi }, h[index]: ${ h[index] }`);
h[index] = mi < h[index] ? mi : h[index];
console.log(`After: t: ${ t }, mi: ${ mi }, h[index]: ${ h[index] }`);
console.log(coinChange([1,2,5], 3));
// keep it infinity or 0 or positive
h[index] = (h[index] === undefined ? mi : mi < h[index] ? mi : h[index]);
result = cc(n, t-n[i], h)
var cc = function(n, t, h) {
// index
const index = t;
// reach 0, re 0
if(t === 0) {
return 0;
} else if(h[index] !== undefined) {
// hash
return h[index];
}
// min
let mi = Infinity;
// loop coin
for(let i=0; i<n.length; i++) {
// posi, get in
if(t-n[i] >= 0) {
// return count or hash
mi = Math.min(mi, cc(n, t-n[i], h)+1);
}
}
// keep it infinity or 0 or positive
h[index] = (h[index] === undefined ? mi : mi < h[index] ? mi : h[index]);
return h[index];
}
var coinChange = function(n, t) {
const res = cc(n, t, {});
const out = res === Infinity ? -1 : res;
return out;
};