📜  两个数组的最大总和组合

📅  最后修改于: 2021-09-17 07:24:26             🧑  作者: Mango

给定两个数组arr1[]arr2[] ,每个数组的大小为N 。任务是从两个数组中选择一些元素,使得没有两个元素具有相同的索引,并且不能从单个数组中选择两个连续的数字。找出上述数字的最大可能总和。
例子:

方法 :
这个问题是基于动态规划的。

  • 如果最后一个元素取自位置(i-1, 1) ,则dp(i, 1)是新选择元素的最大总和。

  • dp(i, 2)是相同的,但最后一个元素的位置为(i-1, 2)
  • dp(i, 3)相同,但我们没有从位置i-1取任何元素

.
递归关系是:

我们实际上并不需要dp( i, 3) ,如果我们将dp(i, 1)更新为max(dp(i, 1), dp(i-1, 1))并将dp(i, 2) 更新max (dp(i, 2), dp(i-1, 2))
因此, dp(i, j)是如果最后一个元素取自位置(i-1, 1 ) 或更小的位置,则选择的元素的最大总和。与dp(i, 2) 相同。因此,上述问题的答案是max(dp(n, 1), dp(n, 2))
下面是上述方法的实现:

C++
// CPP program to maximum sum
// combination from two arrays
#include 
using namespace std;
 
 
// Function to maximum sum
// combination from two arrays
int Max_Sum(int arr1[], int arr2[], int n)
{
    // To store dp value
    int dp[n][2];
     
    // For loop to calculate the value of dp
    for (int i = 0; i < n; i++)
    {
        if(i==0)
        {
            dp[i][0] = arr1[i];
            dp[i][1] = arr2[i];
            continue;
        }
         
       dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + arr1[i]);
       dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + arr2[i]);
    }
     
    // Return the required answer
    return max(dp[n-1][0], dp[n-1][1]);
}
 
// Driver code
int main()
{
    int arr1[] = {9, 3, 5, 7, 3};
    int arr2[] = {5, 8, 1, 4, 5};
 
    int n = sizeof(arr1) / sizeof(arr1[0]);
     
    // Function call
    cout << Max_Sum(arr1, arr2, n);
 
    return 0;
}


Java
// Java program to maximum sum
// combination from two arrays
class GFG
{
 
// Function to maximum sum
// combination from two arrays
static int Max_Sum(int arr1[],
                   int arr2[], int n)
{
    // To store dp value
    int [][]dp = new int[n][2];
     
    // For loop to calculate the value of dp
    for (int i = 0; i < n; i++)
    {
        if(i == 0)
        {
            dp[i][0] = arr1[i];
            dp[i][1] = arr2[i];
            continue;
        }
         
        dp[i][0] = Math.max(dp[i - 1][0],
                            dp[i - 1][1] + arr1[i]);
        dp[i][1] = Math.max(dp[i - 1][1],  
                            dp[i - 1][0] + arr2[i]);
    }
     
    // Return the required answer
    return Math.max(dp[n - 1][0],
                    dp[n - 1][1]);
}
 
// Driver code
public static void main(String[] args)
{
    int arr1[] = {9, 3, 5, 7, 3};
    int arr2[] = {5, 8, 1, 4, 5};
 
    int n = arr1.length;
     
    // Function call
    System.out.println(Max_Sum(arr1, arr2, n));
}
}
 
// This code is contributed
// by PrinciRaj1992


Python3
# Python3 program to maximum sum
# combination from two arrays
 
# Function to maximum sum
# combination from two arrays
def Max_Sum(arr1, arr2, n):
     
    # To store dp value
    dp = [[0 for i in range(2)]
             for j in range(n)]
     
    # For loop to calculate the value of dp
    for i in range(n):
        if(i == 0):
            dp[i][0] = arr1[i]
            dp[i][1] = arr2[i]
            continue
        else:
            dp[i][0] = max(dp[i - 1][0],
                           dp[i - 1][1] + arr1[i])
            dp[i][1] = max(dp[i - 1][1],
                           dp[i - 1][0] + arr2[i])
     
    # Return the required answer
    return max(dp[n - 1][0],
               dp[n - 1][1])
 
# Driver code
if __name__ == '__main__':
    arr1 = [9, 3, 5, 7, 3]
    arr2 = [5, 8, 1, 4, 5]
 
    n = len(arr1)
     
    # Function call
    print(Max_Sum(arr1, arr2, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to maximum sum
// combination from two arrays
using System;
 
class GFG
{
 
// Function to maximum sum
// combination from two arrays
static int Max_Sum(int []arr1,
                   int []arr2, int n)
{
    // To store dp value
    int [,]dp = new int[n, 2];
     
    // For loop to calculate the value of dp
    for (int i = 0; i < n; i++)
    {
        if(i == 0)
        {
            dp[i, 0] = arr1[i];
            dp[i, 1] = arr2[i];
            continue;
        }
         
        dp[i, 0] = Math.Max(dp[i - 1, 0],
                            dp[i - 1, 1] + arr1[i]);
        dp[i, 1] = Math.Max(dp[i - 1, 1],
                            dp[i - 1, 0] + arr2[i]);
    }
     
    // Return the required answer
    return Math.Max(dp[n - 1, 0],
                    dp[n - 1, 1]);
}
 
// Driver code
public static void Main()
{
    int []arr1 = {9, 3, 5, 7, 3};
    int []arr2 = {5, 8, 1, 4, 5};
 
    int n = arr1.Length;
     
    // Function call
    Console.WriteLine(Max_Sum(arr1, arr2, n));
}
}
 
// This code is contributed
// by anuj_67..


Javascript


输出:
29

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程