给定两个长度相等的数组arr1[]和arr2[] ,任务是通过从两个数组中选择元素来找到任何可能的子集的最大和,这样子集中的任何两个元素都不应该是连续的。
例子:
Input: arr1[] = {-1, -2, 4, -4, 5}, arr2[] = {-1, -2, -3, 4, 10}
Output: 14
Explanation:
Required subset {4, 10}. Therefore, sum = 4 + 10 = 14.
Input: arr1[] = {2, 5, 4, 2000}, arr2[] = {-2000, 100, 23, 40}
Output: 2100
朴素的方法:最简单的方法是从两个给定的数组中生成所有可能的子集,这样没有两个相邻的元素是连续的,并计算每个子集的总和。最后,打印可能的最大和。
时间复杂度: 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.
Javascript
输出:
14
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live