查找其总和等于给定目标的整数数组的最高子集

编程入门 行业动态 更新时间:2024-10-09 18:21:18
本文介绍了查找其总和等于给定目标的整数数组的最高子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在一个数组中找到一个值的子集,这些值的总和等于一个给定的数字,然后返回该子集的数组.我需要return数组包含最大可能的最大值.

I am trying to find a subset of values within an array which add up to a given number and return an array of that subset. I need the return array to contain the highest possible largest value.

整数数组将始终按升序排列,但不会始终有一个有效的答案.如果没有有效答案,我需要返回无有效答案"行的字符串.

The integer array will always be in ascending order but there will not always be a valid answer. If there is no valid answer I need to return a string along the lines of 'No valid answer'.

例如:

如果int数组是:[1, 2, 3, 5, 7, 9, 11],而目标是19-我希望返回数组是[1, 7, 11]. 我不想返回[9, 7, 3],因为11大于9,并且我不想返回[3, 5, 11],因为7大于5.

If the int array is: [1, 2, 3, 5, 7, 9, 11] and the target is 19 - I want the return array to be [1, 7, 11]. I would not like to return [9, 7, 3] because 11 is larger than 9, and I would not like to return [3, 5, 11] because 7 is larger than 5.

我假设我将需要在递归函数中使用for循环来获取答案,但一直无法弄清楚该怎么做.

I am assuming that I will need a for loop within a recursive function to get my answer but have been unable to figure out how to do it.

我在Java中发现了该问题的各种变化: codereview.stackexchange/questions/36214/find-all-subsets-of-int-array-hose-sums-等于给定的目标

I have found variations of the question in Java: codereview.stackexchange/questions/36214/find-all-subsets-of-an-int-array-whose-sums-equal-a-given-target

我也在JavaScript中找到了一个返回值对的解决方案(尽管这很简单): codereview.stackexchange/questions/74152/given-an-array-of-integers-return-all-pairs-that-and-upto-to-100

I have also found a solution in JavaScript which returns pairs of values (although this is pretty straight forward): codereview.stackexchange/questions/74152/given-an-array-of-integers-return-all-pairs-that-add-up-to-100

我当时在想,您需要从数组的最后一个元素开始循环,然后向后循环遍历该数组,以检查是否有两个数字之和作为目标的组合.

I was thinking that you would need to start your loop with the last element of the array and loop backwards through the array to check if there is a combination of 2 numbers whose sum is the target.

如果没有一对,则需要从最高的一对数字开始,这些数字的总和小于目标值,然后遍历数组以查找是否存在组合.

If there is not a pair, you would need to start with the highest pair of numbers whose sum is less that the target and loop though the array to find if there is a combination.

需要继续此操作,直到找到子集或您发现没有有效答案为止.

This operation would need to be continued until the subset is found or you discover that there is no valid answer.

请帮助!

推荐答案

递归的基本模式是:

  • 返回简单案例的已知值.对于此问题,简单的情况是(a)目标为0(解决方案为空数组)和(b)目标为负(失败).
  • 递归并根据答案返回一些计算.对于这个问题,我们查看每个候选值,然后递减一个目标值并递减该值,然后返回一个由递归结果加上当前元素组成的数组.
  • 所以:

    // Find subset of a that sums to n. function subset_sum(n, a) { // SIMPLE CASES if (n < 0) return null; // Nothing adds up to a negative number if (n === 0) return []; // Empty list is the solution for a target of 0 // RECURSIVE CASE a = a.slice(); while (a.length) { // Try remaining values var v = a.shift(); // Take next value var s = subset_sum(n - v, a); // Find solution recursively if (s) return s.concat(v); // If solution, return } }

    要强制执行您希望首先偏爱较高值的规则,请将此函数传递一个按降序预排序的值数组.

    To enforce the rule that you want to prefer higher values first, pass this function an array of values that is pre-sorted in descending order.

    > subset_sum(19, [11, 9, 7, 5, 3, 2, 1]) < [1, 7, 11]

    更多推荐

    查找其总和等于给定目标的整数数组的最高子集

    本文发布于:2023-11-29 15:51:23,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1646854.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:子集   整数   数组   总和   目标

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!