简体   繁体   English

查找列表中小于给定值的最大整数和(代码跟踪)

[英]Finding the largest sum of integers in a list less than a given value (code tracing)

Had this problem in a review guide given by CS professor. CS教授给出的评论指南中有这个问题。 I ended up solving the problem relatively naively by computing all possible sums of all integers in the list via recursive backtracking in a helper, storing these in a new list, then iterating through that list to find the max sum less than the given number.我最终通过在帮助程序中通过递归回溯计算列表中所有整数的所有可能总和,将它们存储在一个新列表中,然后遍历该列表以找到小于给定数字的最大总和,从而相对天真地解决了这个问题。

We were given the instructor's far more eloquent solution, but I'm having a lot of trouble dissecting it.我们得到了讲师更多的 eloquent 解决方案,但我在剖析它时遇到了很多麻烦。 Hopefully this is an acceptable place to ask for help breaking down what's going on (if not, apologies).希望这是一个可以接受的地方,可以寻求帮助以了解正在发生的事情(如果没有,请道歉)。

private static int largestSum(List<Integer> list, int n) {
    if (list.isEmpty()) {
        return 0;
    } else {
        int choice = list.get(0);
        list.remove(0);
        // Don't include choice in final sum
        int largest = sumHelper(list, n);
        if (choice < n) {
            // Include choice in final sum
            int included = choice + sumHelper(list, n - choice);
            // Use whichever result is bigger
            largest = Math.max(largest, included);
        }
        // Return list to original state
        list.add(0, choice);
        return largest;
        }
}

I understand the base case (we're iterating through the list by popping off the first element and recursing on the remainder, so when we hit the base case, we stop recursing and don't want to add anything hence the zero; it also addresses empty list).我理解基本情况(我们通过弹出第一个元素并递归剩余部分来迭代列表,所以当我们遇到基本情况时,我们停止递归并且不想添加任何内容因此为零;它也地址空列表)。 However, I'm still super unclear about the recursive case.但是,我仍然对递归案例非常不清楚。 I've stared at the code and tried to trace it for a while now, but wrapping my head around the recursion is proving very difficult.我已经盯着代码并试图追踪它一段时间,但我的头围绕递归被证明是非常困难的。 Can someone walk me through it?有人可以带我过去吗?

Each number in the list can have two possibility:列表中的每个数字都有两种可能性:

  1. To not be the part of the largest sum.不成为最大金额的一部分。 Hence we are removing the number from list and Iterating over remaining list.Since Number is Not selected, NO CHANGES IN THE GIVEN VALUE.因此,我们从列表中删除数字并迭代剩余的列表。由于未选择数字,因此给定值没有变化。

    int largest = sumHelper(list, n); int 最大 = sumHelper(list, n);

  2. To be the part of the largest sum.成为金额最大的部分。 Hence we are checking if the number is less than the given value ( only then we will include that number), and including it in largest sum.因此,我们正在检查数字是否小于给定值(只有这样我们才会包含该数字),并将其包含在最大总和中。 Then we are Iterating over remaining list, with GIVEN VALUE modified to Value = Value - NumberSelected (Since we are including the current number, so SUM is modified).然后我们迭代剩余的列表,将 GIVEN VALUE 修改为 Value = Value - NumberSelected (因为我们包括当前数字,所以修改了 SUM)。

    if (choice < n) { int included = choice + sumHelper(list, n - choice); if (choice < n) { 包含的 int = 选择 + sumHelper(list, n - 选择); largest = Math.max(largest, included);最大 = Math.max(最大,包括); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM