Javascript 查找添加值后将等于x的元素的索引
我有一个数组,看起来像这样:Javascript 查找添加值后将等于x的元素的索引,javascript,arrays,Javascript,Arrays,我有一个数组,看起来像这样: var x = 17; var arr = [{ value:2, quantity: 4 }, { value:8, quantity: 1 }, { value:3, quantity: 3 }]; 我的问题是如何找到那些元素的索引,在添加它们的值后,这些索引将等于x数字。在这种情况下,回报将是: [1, 3, 3, 3] 当然,也可以使用[0,0,0,1,2]或[0,0,0,0,2,2]来完成,但返回数组的长度越短越好。有更有效的方法,但这是一个非常明显/
var x = 17;
var arr = [{ value:2, quantity: 4 }, { value:8, quantity: 1 }, { value:3, quantity: 3 }];
我的问题是如何找到那些元素的索引,在添加它们的值后,这些索引将等于x
数字。在这种情况下,回报将是:
[1, 3, 3, 3]
当然,也可以使用
[0,0,0,1,2]
或[0,0,0,0,2,2]
来完成,但返回数组的长度越短越好。有更有效的方法,但这是一个非常明显/干净的解决方案。我们将把它当作一个线性方程,其中,combo
包含arr
中每个值的系数:
// your initial x and arr
var x = 17;
var arr = [{ value:2, quantity: 4 }, { value:8, quantity: 1 }, { value:3, quantity: 3 }];
// maximums[i] is the maximum amount of arr[i]'s you can ever
// have in any combination
var maximums = arr.map(function(item){ return Math.floor(x / item.value) });
// an array of the current coefficients we're trying. combo[i]
// corresponds to the coefficient for arr[i]
// we will start off with all coefficients set to 0 and
// increase each one by 1 as we go along
var combo = arr.map(function(){ return 0 });
// the sum we get with the current coefficients
var sum = 0;
// take the current coefficients in combo and try the next
// coefficient from left-to-right, we know when to stop
// trying to increase a given coefficient when it exceeds
// its corresponding value in the maximums array
function next() {
for(var i = 0; i < combo.length; i++) {
combo[i]++;
// we increased the coeff. so increase the sum
sum += arr[i].value;
if(combo[i] <= maximums[i]) {
// the coefficient is smaller/equal to its max size, so we're done
break;
}else{
// we've maxed out the right most coeff. so bail
if(i == combo.length-1) return false;
// reset this coefficient, remove it from sum & cont. loop
sum -= arr[i].value * combo[i];
combo[i] = 0;
}
}
return true;
}
// now we just enumerate all combinations until the sum equals x
while(next()) {
if(sum == x) break;
}
// if no combination is found, abort
if(sum != x) {
console.log('not possible');
// otherwise print the combination that works
}else{
for(var i = 0; i < combo.length; i++) {
if(combo[i] == 0) continue;
console.log(combo[i] + ' of (' + JSON.stringify(arr[i]) + ')');
}
}
//您的初始x和arr
var x=17;
var arr=[{value:2,数量:4},{value:8,数量:1},{value:3,数量:3}];
//maximums[i]是您可以使用的最大arr[i]数量
//有任何组合
var max=arr.map(函数(项){return Math.floor(x/item.value)});
//我们正在尝试的电流系数数组。组合[i]
//对应于arr[i]的系数
//我们将首先将所有系数设置为0和
//我们继续前进时,将每个值增加1
var combo=arr.map(函数(){return 0});
//我们得到的电流系数之和
var总和=0;
//取combo中的当前系数,然后尝试下一个
//系数从左到右,我们知道何时停止
//试图在给定系数超过时增加该系数
//它在最大值数组中的对应值
函数next(){
for(变量i=0;i
阵列中可以有多少不同的项目?实际上没有具体的限制。arr
数组是不可预测的。
function coeffsUsed() {
var count = 0;
for(var i = 0; i < combo.length; i++) {
if(combo[i] > 0) count++;
}
return count;
}
// now we just enumerate all combinations until the sum equals x
var smallestCombo = {};
var smallest = -1;
while(next()) {
if(sum == x) {
var count = coeffsUsed();
if(smallest == -1 || count < smallest) {
smallest = count;
smallestCombo = combo.slice(0); // clones the array
}
}
}
// if no combination is found, abort
if(smallest == -1) {
console.log('not possible');
// otherwise print the combination that works
}else{
for(var i = 0; i < smallestCombo.length; i++) {
if(smallestCombo[i] == 0) continue;
console.log(smallestCombo[i] + ' of (' + JSON.stringify(arr[i]) + ')');
}
}