要打印的数组中非相邻元素的最大和

时间:2019-04-15 12:12:05

标签: java algorithm dynamic-programming

有一个整数数组{1,2,3,-1,-3,2,5},我的工作是打印导致子数组最大和的元素,得到的和是通过加数组中不相邻的元素。

我使用动态编程编写了给出最大和的代码。但无法打印元素。

public static int maxSum(int arr[]) 
{       
    int excl = 0;
    int incl = arr[0];
    for (int i = 1; i < arr.length; i++) 
    {
        int temp = incl;
        incl = Math.max(Math.max(excl + arr[i], arr[i]), incl);
        excl = temp;
    }
    return incl;
}

示例:

  • {1,2,3,-1,-3,2,5}应该返回{1,3,5},因为最大金额为 9
  • {4,5,4,3}有两个和{4,4}{5,3},在对两个数组进行排序时,我们得到{4,4}{3,5},因为3 <4我们打印了{{1 }}。(包含第一个最小元素的数组)。

2 个答案:

答案 0 :(得分:6)

您可以保留一个数组来跟踪用于index of elements的{​​{1}}。

我已经使用父数组修改了您的代码以跟踪该代码。另外,根据我的理解,我更改了一些变量名。

add to the current element

我相信可以对代码进行很多清理,但这是以后的工作:)

输出

public static void maxSum(int[] arr){ int n = arr.length; int[] parent = new int[n]; parent[0] = -1; int lastSum = 0; // last sum encountered int lastPos = -1; // position of that last sum int currSum = arr[0]; // current sum int currPos = 0; // position of the current sum for (int i = 1; i < n; i++) { parent[i] = lastPos; // save the last sum's position for this element // below this it is mostly similar to what you have done; // just keeping track of position too. int probableSum = Integer.max(arr[i] + lastSum, arr[i]); int tSum = currSum; int tPos = currPos; if(probableSum > currSum){ currSum = probableSum; currPos = i; } lastSum = tSum; lastPos = tPos; } System.out.println(currSum); // print sum System.out.println(Arrays.toString(parent)); // print parent array; for debugging purposes. // logic to print the elements int p = parent[n - 1]; System.out.print(arr[n - 1] + " "); while (p != -1) { System.out.print(arr[p] + " "); p = parent[p]; } }

{1,2,3,-1,-3,2,5} => 5 3 1

更新。添加了一些代码说明

{4,5,4,3} => 3 5lastSum的值在循环执行期间发生变化。通过观察其值在循环内如何变化,可以最好地理解它们。

在循环currSum的第i个迭代的开始期间,拥有可以添加到第lastSum个元素的最大值;因此基本上可以通过迭代第i个元素来获得最大值。 i-2拥有可通过迭代第currSum个元素而获得的最大值。

在循环结束时,将i-1添加到第lastSum元素,并指定为i。如果currSum小于0,则将第lastSum个元素本身指定为i。现在currSum的旧值称为currSum

lastSumlastPos拥有各自总和值的最新索引。

在下面显示的每个迭代的所有状态下,最右边的和表示迭代开始时的currPoscurrSum左侧的值表示currSum。它们的索引位置分别记录在lastSumcurrPos中。

lastPos保留所使用的par[]的最后一个索引的值。此数组随后用于构造形成最大不相邻总和的实际元素集。

lastSum

initially

idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1
par =     -1

i=1 iteration state
idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1,  ?
par =     -1,  !

// before update
currSum = 1, currPos = 0
lastSum = 0, lastPos = -1

// updating
par[1] = lastPos = -1
probableSum = max(2 + 0, 2)  = 2 // max(arr[i] + lastSum, arr[i])
? = max(1, 2) = 2 // max(currSum, probableSum)
! = i = 1

// after update
lastSum = currSum = 1
lastPos = currPos = 0
currSum = ? = 2
currPos = ! = 1

i=2 iteration state
idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1,  2  ?
par =     -1, -1  !

// before update
currSum = 2, currPos = 1
lastSum = 1, lastPos = 0

// updating
par[2] = lastPos = 0
probableSum = max(3 + 1, 3) = 4 // max(arr[i] + lastSum, arr[i])
? = max(2, 4) = 4 // max(currSum, probableSum)
! = i = 2

// after update
lastSum = currSum = 2
lastPos = currPos = 1
currSum = ? = 4
currPos = ! = 2

i = 3 iteration state
idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1,  2  4   ?
par =     -1, -1  0   !

// before update
currSum = 4, currPos = 2
lastSum = 2, lastPos = 1

//updating
par[3] = lastpos = 1
probableSum = max(-1 + 2, -1) = 1 // max(arr[i] + lastSum, arr[i])
? = max(4, 1) = 4 // max(currSum, probableSum) ; no update in ?'s value
! = currPos = 2 // as ?'s value didn't update

// after update
lastSum = currSum = 4
lastPos = currPos = 2
currSum = ? = 4
currPos = ! = 2

i = 4 iteration
idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1,  2  4   4   ?
par =     -1, -1  0   1   !

// before update
currSum = 4, currPos = 2
lastSum = 4, lastPos = 2

// updating
par[4] = lastPos = 2
probableSum = max(-3 + 4, -3) = 1 // max(arr[i] + lastSum, arr[i])
? = max(4, 1) = 4 // max(currSum, probableSum) ; no update in ?'s value
! = currPos = 2 // as ?'s value didn't update

// after update
lastSum = currSum = 4
lastPos = currPos = 2
currPos = ? = 4
currPos = ! = 2

i = 5 iteration
idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1,  2  4   4   4  ?
par =     -1, -1  0   1   2  !

// before update
currSum = 4, currPos = 2
lastSum = 4, lastPos = 2

// updating
par[5] = lastPos = 2
probableSum = max(2 + 4, 2) = 6 // max(arr[i] + lastSum, arr[i])
? = max(4, 6) = 6 // max(currSum, probableSum)
! = i = 5

// after update
lastSum = currSum = 4
lastPos = currPos = 2
currPos = ? = 6
currPos = ! = 5

i = 6 iteration state
idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1,  2  4   4   4  6  ?
par =     -1, -1  0   1   2  2  !

// before update
currSum = 6, currPos = 5
lastSum = 4, lastPos = 2

// updating
par[6] = lastPos = 2
probableSum = max(5 + 4, 5) = 9 // max(arr[i] + lastSum, arr[i])
? = max(6, 9) = 9 // max(currSum, probableSum)
! = i = 6

// after update
lastSum = currSum = 6
lastPos = currPos = 5
currPos = ? = 9
currPos = ! = 6

通过使用par []并循环直到par [p]!= -1,我们可以获得元素索引,这些元素实际上构成了实际所需元素的集合。直接检查代码。

例如

after all iteration state
idx = -1,  0,  1, 2,  3,  4, 5, 6
arr =  0,  1,  2, 3, -1, -3, 2, 5
sum =  0   1,  2  4   4   4  6  9
par =     -1, -1  0   1   2  2  2

答案 1 :(得分:5)

我更喜欢酋长的解决方案,但这是另一种方法:

/** returns a list of indices which contain the elements that make up the max sum */
public static List<Integer> maxSum(int arr[]) {
    int priorMaxSum = 0;
    List<Integer> priorMaxSumList = new ArrayList<>();

    // initial max sum
    int maxSum = arr[0];
    List<Integer> maxSumList = new ArrayList<>();
    maxSumList.add(0);

    for (int i = 1; i < arr.length; i++) {
        final int currSum;
        final List<Integer> currSumList;
        if (priorMaxSum > 0) {
            // if the prior sum was positive, then continue from it
            currSum = priorMaxSum + arr[i];
            currSumList = new ArrayList<>(priorMaxSumList);
        } else {
            // if the prior sum was not positive, then throw it out and start new
            currSum = arr[i];
            currSumList = new ArrayList<>();
        }
        currSumList.add(i);

        // update prior max sum and list
        priorMaxSum = maxSum;
        priorMaxSumList = new ArrayList<>(maxSumList);

        if (currSum > maxSum) {
            // update max sum
            maxSum = currSum;
            maxSumList = currSumList;
        }
    }
    return maxSumList;
}

public static void main(String[] args) throws Exception {
    int[] a = {1, 2, 3, -5, -3, 2, 5};

    List<Integer> l = maxSum(a);
    System.out.println(
            "indices {" + l.stream().map(String::valueOf).collect(Collectors.joining(", ")) + "}");
    System.out.println("values  {"
            + l.stream().map(i -> String.valueOf(a[i])).collect(Collectors.joining(", ")) + "}");
}