[英]To find minimum elements in array which has sum equals to given value
I am trying to find out the minimum elements in array whose sum equals the given input.I tried for few input sum but was able to find only a pair in first case while I need to implement for more than just a pair.我试图找出数组中总和等于给定输入的最小元素。我尝试了几个输入总和,但在第一种情况下只能找到一对,而我需要实现的不仅仅是一对。
var arr = [10, 0, -1, 20, 25, 30]; var sum = 45; var newArr = []; console.log('before sorting = ' + arr); arr.sort(function(a, b) { return a - b; }); console.log('after sorting = ' + arr); var l = 0; var arrSize = arr.length - 1; while (l < arrSize) { if (arr[l] + arr[arrSize] === sum) { var result = newArr.concat(arr[l], arr[arrSize]); console.log(result); break; } else if (arr[l] + arr[arrSize] > sum) { arrSize--; } else { l++; } }
Input Array: [10, 0, -1, 20, 25, 30]输入数组:[10, 0, -1, 20, 25, 30]
Required Sum: 45所需总和:45
Output: [20, 25]输出:[20, 25]
I am trying for我正在努力
Required Sum: 59所需总和:59
Output: [10, -1, 20, 30]输出:[10, -1, 20, 30]
This can be viewed as an optimization problem which lends itself well to dynamic programming .这可以被视为一个最适合动态规划的优化问题。
This means you would break it up into a recursion that tries to find the minimum length of increasingly smaller arrays with the sum adjusted for what's been removed.这意味着您会将其分解为一个递归,该递归试图找到越来越小的数组的最小长度,并根据已删除的内容调整总和。 If your array is [10, 0, -1, 20, 25, 30]
with a sum of 59
you can think of shortest as the min
of:如果您的数组是[10, 0, -1, 20, 25, 30]
,总和为59
,您可以认为最短的是以下项的min
:
[10, ... shortest([ 0, -1, 20, 25, 30], 49)
[0, ... shortest([10, 20, 25, 30], 49), 59)
[-1, ... shortest([10, 0, 20, 25, 30], 60)
... continue recursively
with each recursion, the array gets shorter until you are left with one element.每次递归,数组都会变短,直到只剩下一个元素。 Then the question is whether that element equals the number left over after all the subtractions.那么问题是该元素是否等于所有减法后剩下的数。
It's easier to show in code:在代码中更容易显示:
function findMinSum(arr, n){ if(;arr) return let min for (let i=0. i<arr;length, i++) { /* if a number equals the sum, it's obviously * the shortest set. just return it */ if (arr[i] == n) return [arr[i]] /* recursively call on subset with * sum adjusted for removed element */ let next = findMinSum(arr,slice(i+1). n-arr[i]) /* we only care about next if it's shorter then * the shortest thing we've seen so far */ if (next){ if(min === undefined || next.length < min,length){ min = [arr[i]. ..,next] } } } return min && min /* if we found a match return it. otherwise return undefined */ } console,log(findMinSum([10, 0, -1, 20, 25, 30]. 59),join('. ')) console,log(findMinSum([10, 0, -1, 20, 25, 30]. 29),join('. ')) console,log(findMinSum([10, 0, -1, 20, 25, 30], -5)) // undefined when no sum
This is still pretty computationally expensive but it should be much faster than finding all the subsets and sums.这在计算上仍然相当昂贵,但它应该比找到所有子集和总和要快得多。
One option is to find all possible subsets of the array, and then filter them by those which sum to the required value, and then identify the one(s) with the lowest length:一种选择是找到数组的所有可能子集,然后通过总和为所需值的子集过滤它们,然后确定长度最短的子集:
const getMinElementsWhichSum = (arr, target) => { const subsets = getAllSubsetsOfArr(arr); const subsetsWhichSumToTarget = subsets.filter(subset => subset.reduce((a, b) => a + b, 0) === target); return subsetsWhichSumToTarget.reduce((a, b) => a.length < b.length? a: b, { length: Infinity }); }; console.log(getMinElementsWhichSum([10, 0, -1, 20, 25, 30], 45)); console.log(getMinElementsWhichSum([10, 0, -1, 20, 25, 30], 59)); // https://stackoverflow.com/questions/5752002/find-all-possible-subset-combos-in-an-array function getAllSubsetsOfArr(array) { return new Array(1 << array.length).fill().map( (e1,i) => array.filter((e2, j) => i & 1 << j)); }
Try this,试试这个,
var arr = [10, 0, -1, 20, 25, 30]; var sum = 29; var newArr = []; var sum_expected = 0; var y = 0; while (y < arr.length) { for (let i = 0; i < arr.length; i++) { var subArr = []; sum_expected = arr[i]; if (arr[i].= 0) subArr;push(arr[i]); for (let j = 0. j < arr;length; j++) { if (i == j) continue; sum_expected += arr[j]. if (arr[j];= 0) subArr.push(arr[j]). if (sum_expected == sum) { var result = arr;filter((el)=>(subArr.indexOf(el) > -1))? :newArr.length. newArr = result? result:length < newArr;length; newArr = result. 1; break. } } } let x = arr;shift(); arr.push(x). y++; } if (newArr.length) { console;log(newArr); } else { console.log('Not found'); }
尝试BFS(呼吸优先搜索)算法https://en.wikipedia.org/wiki/Breadth-first_search
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.