📜  最大化两个没有连续值的数组的子集总和

📅  最后修改于: 2021-05-13 23:26:40             🧑  作者: Mango

给定两个长度相等的数组arr1 []arr2 [] ,任务是通过从两个数组中选择元素来找到任何可能的子集的最大和,以使子集中的任何两个元素都不应该是连续的。

例子:

天真的方法:最简单的方法是从两个给定的数组中生成所有可能的子集,以使没有两个相邻的元素是连续的,并计算每个子集的总和。最后,打印可能的最大金额。
时间复杂度: O(N * 2 N )
辅助空间: O(2 N )

高效方法:可以使用动态编程来优化上述方法。请按照以下步骤解决问题:

  • 初始化大小为N的辅助数组dp []
  • 此处, dp [i]存储两个数组中一个子集的最大可能和,以使两个元素都不连续。
  • 声明一个函数maximumSubsetSum():
    • 基本案例:
      • dp [1] = max(arr1 [1],arr2 [1])。
      • dp [2] = max(max(arr1 [1],arr2 [1]),max(arr1 [2],arr2 [2]))。
    • 对于所有其他情况,会出现以下三个条件:
      • dp [i] = max(arr1 [i],arr2 [i],arr1 [i] + dp [i-2],arr2 [i] + dp [i-2],dp [i-1])。
  • 最后,将dp [N]打印为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate maximum subset sum
void maximumSubsetSum(int arr1[],int arr2[], int length)
{
 
    // Initialize array to store dp states
    int dp[length+1];
 
    // Base Cases
    if (length == 1)
    {
        cout << (max(arr1[0], arr2[0]));
        return;
    }
 
    if (length == 2)
    {
        cout << (max(max(arr1[1], arr2[1]), max(arr1[0], arr2[0])));
        return;
    }
    else
    {
 
        // Pre initializing for dp[0] & dp[1]
        dp[0] = max(arr1[0], arr2[0]);
        dp[1] = max(max(arr1[1], arr2[1]), max(arr1[0], arr2[0]));
 
        int index = 2;
        while (index < length)
        {
 
            // Calculating dp[index] based on
            // above formula
            dp[index] = max(max(arr1[index], arr2[index]),
                max(max(arr1[index] + dp[index - 2],
                        arr2[index] + dp[index - 2]),
                    dp[index - 1]));
            ++index;
        }
 
        // Print maximum subset sum
        cout<<(dp[length - 1]);
    }
}
 
// Driver Code
int main()
{
   
  // Given arrays
  int arr1[] = { -1, -2, 4, -4, 5 };
  int arr2[] = { -1, -2, -3, 4, 10 };
 
  // Length of the array
  int length = 5;
  maximumSubsetSum(arr1, arr2, length);
  return 0;
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
class GFG {
 
    // Function to calculate maximum subset sum
    static void maximumSubsetSum(int arr1[],
                                 int arr2[],
                                 int length)
    {
 
        // Initialize array to store dp states
        int dp[] = new int[length + 1];
 
        // Base Cases
        if (length == 1) {
            System.out.print(
                Math.max(arr1[0], arr2[0]));
            return;
        }
 
        if (length == 2) {
            System.out.print(
                Math.max(
                    Math.max(arr1[1], arr2[1]),
                    Math.max(arr1[0], arr2[0])));
            return;
        }
        else {
 
            // Pre initializing for dp[0] & dp[1]
            dp[0] = Math.max(arr1[0], arr2[0]);
            dp[1] = Math.max(
                Math.max(arr1[1], arr2[1]),
                Math.max(arr1[0], arr2[0]));
 
            int index = 2;
            while (index < length) {
 
                // Calculating dp[index] based on
                // above formula
                dp[index] = Math.max(
                    Math.max(arr1[index], arr2[index]),
                    Math.max(
                        Math.max(
                            arr1[index] + dp[index - 2],
                            arr2[index] + dp[index - 2]),
                        dp[index - 1]));
                ++index;
            }
 
            // Print maximum subset sum
            System.out.print(dp[length - 1]);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given arrays
        int arr1[] = { -1, -2, 4, -4, 5 };
        int arr2[] = { -1, -2, -3, 4, 10 };
 
        // Length of the array
        int length = arr1.length;
 
        maximumSubsetSum(arr1, arr2, length);
    }
}


Python3
# Python program of the above approach
 
# Function to calculate maximum subset sum
def maximumSubsetSum(arr1, arr2, length) :
 
    # Initialize array to store dp states
    dp = [0] * (length+1)
 
    # Base Cases
    if (length == 1) :     
        print(max(arr1[0], arr2[0]))
        return
    if (length == 2) :
        print(max(max(arr1[1], arr2[1]), max(arr1[0], arr2[0])))
        return 
    else  :
 
        # Pre initializing for dp[0] & dp[1]
        dp[0] = max(arr1[0], arr2[0])
        dp[1] = max(max(arr1[1], arr2[1]), max(arr1[0], arr2[0]))
        index = 2
        while (index < length) :
 
            # Calculating dp[index] based on
            # above formula
            dp[index] = max(max(arr1[index], arr2[index]),
                max(max(arr1[index] + dp[index - 2],
                        arr2[index] + dp[index - 2]),
                    dp[index - 1]))
            index += 1
         
        # Prmaximum subset sum
        print(dp[length - 1])
     
# Driver Code
 
# Given arrays
arr1 = [ -1, -2, 4, -4, 5 ]
arr2 = [ -1, -2, -3, 4, 10 ]
 
# Length of the array
length = 5
maximumSubsetSum(arr1, arr2, length)
 
# This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to calculate maximum subset sum
  static void maximumSubsetSum(int[] arr1,
                               int[] arr2,
                               int length)
  {
 
    // Initialize array to store dp states
    int[] dp = new int[length + 1];
 
    // Base Cases
    if (length == 1) {
      Console.WriteLine(Math.Max(arr1[0], arr2[0]));
      return;
    }
 
    if (length == 2)
    {
      Console.WriteLine(Math.Max(
        Math.Max(arr1[1], arr2[1]),
        Math.Max(arr1[0], arr2[0])));
      return;
    }
    else
    {
 
      // Pre initializing for dp[0] & dp[1]
      dp[0] = Math.Max(arr1[0], arr2[0]);
      dp[1] = Math.Max(Math.Max(arr1[1], arr2[1]),
                       Math.Max(arr1[0], arr2[0]));
 
      int index = 2;
      while (index < length) {
 
        // Calculating dp[index] based on
        // above formula
        dp[index] = Math.Max(Math.Max(arr1[index], arr2[index]),
                             Math.Max(Math.Max(arr1[index] +
                                               dp[index - 2],
                                               arr2[index] +
                                               dp[index - 2]),
                                      dp[index - 1]));
        ++index;
      }
 
      // Print maximum subset sum
      Console.WriteLine(dp[length - 1]);
    }
  }
 
  // Driver Code
  static public void Main()
  {
 
    // Given arrays
    int[] arr1 = { -1, -2, 4, -4, 5 };
    int[] arr2 = { -1, -2, -3, 4, 10 };
 
    // Length of the array
    int length = arr1.Length;
 
    maximumSubsetSum(arr1, arr2, length);
  }
}
 
// This code is contributed by code_hunt.


输出:
14

时间复杂度: O(N)
辅助空间: O(N)