📌  相关文章
📜  根据给定条件最大化可以从两个给定数组中获得的总和

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

给定两个大小为N 的数组A[]B[] ,任务是根据以下条件找到可以得到的最大和:

  • A[i]B[i]都不能包含在总和中 ( 0 ≤ i ≤ N – 1 )。
  • 如果将B[i]添加到总和中,则B[i – 1]A[i – 1]不能包含在总和中 ( 0 ≤ i ≤ N – 1 )。

例子:

方法:这个问题有最优子结构和重叠子问题。因此,可以使用动态规划来解决该问题。
请按照以下步骤解决问题:

  • 初始化一个 arra,比如dp[n][2],其中dp[i][0]存储最大和,如果元素A[i]被考虑在内, dp[i][1]存储最大和,如果B[ i]被考虑在内。
  • 使用变量i在范围[0, N – 1] 中迭代并执行以下步骤:
    • 如果i等于0 ,则将dp[i][0]的值修改为A[i]并将dp[i][1] 的值修改B[i]
    • 否则,执行以下操作:
      • dp[i][0]的值修改为max(dp[i – 1][0], dp[i – 1][1]) + A[i]
      • dp[i][1]的值修改为max(dp[i – 1], max(dp[i – 1][0], max(dp[i – 2][0], dp[i – 2]) ][1]) + B[i]))
  • 完成上述步骤后,打印max(dp[N-1][0], dp[N-1][1])作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum sum
// that can be obtained from two given
// based on the following conditions
int MaximumSum(int a[], int b[], int n)
{
    // Stores the maximum
    // sum from 0 to i
    int dp[n][2];
 
    // Initialize the value of
    // dp[0][0] and dp[0][1]
    dp[0][0] = a[0];
    dp[0][1] = b[0];
 
    // Traverse the array A[] and B[]
    for (int i = 1; i < n; i++) {
        // If A[i] is consiered
        dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i];
 
        // If B[i] is not considered
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]);
 
        // If B[i] is considered
        if (i - 2 >= 0) {
            dp[i][1] = max(dp[i][1],
                           max(dp[i - 2][0],
                               dp[i - 2][1])
                               + b[i]);
        }
        else {
            // If i = 1, then consider the
            // value of  dp[i][1] as b[i]
            dp[i][1] = max(dp[i][1], b[i]);
        }
    }
 
    // Return maximum Sum
    return max(dp[n - 1][0], dp[n - 1][1]);
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 10, 1, 10, 10 };
    int B[] = { 5, 50, 1, 5 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    cout << MaximumSum(A, B, N);
    return 0;
}


Java
// Java program for the above approach
 
class GFG {
    // Function to find the maximum sum
    // that can be obtained from two given
    // based on the following conditions
    public static int MaximumSum(int a[], int b[], int n) {
        // Stores the maximum
        // sum from 0 to i
        int[][] dp = new int[n][2];
 
        // Initialize the value of
        // dp[0][0] and dp[0][1]
        dp[0][0] = a[0];
        dp[0][1] = b[0];
 
        // Traverse the array A[] and B[]
        for (int i = 1; i < n; i++) {
            // If A[i] is consiered
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]) + a[i];
 
            // If B[i] is not considered
            dp[i][1] = Math.max(dp[i - 1][0], dp[i - 1][1]);
 
            // If B[i] is considered
            if (i - 2 >= 0) {
                dp[i][1] = Math.max(dp[i][1], Math.max(dp[i - 2][0], dp[i - 2][1]) + b[i]);
            } else
            {
               
                // If i = 1, then consider the
                // value of dp[i][1] as b[i]
                dp[i][1] = Math.max(dp[i][1], b[i]);
            }
        }
 
        // Return maximum Sum
        return Math.max(dp[n - 1][0], dp[n - 1][1]);
    }
 
    // Driver Code
    public static void main(String args[]) {
        // Given Input
        int A[] = { 10, 1, 10, 10 };
        int B[] = { 5, 50, 1, 5 };
        int N = A.length;
 
        // Function Call
        System.out.println(MaximumSum(A, B, N));
    }
}
 
// This  code is contributed by _saurabh_jaiswal.


Python3
# Python3 program for the above approach
 
# Function to find the maximum sum
# that can be obtained from two given
# arrays based on the following conditions
def MaximumSum(a, b, n):
   
    # Stores the maximum
    # sum from 0 to i
    dp = [[-1 for j in range(2)]
              for i in range(n)]
     
    # Initialize the value of
    # dp[0][0] and dp[0][1]
    dp[0][0] = a[0]
    dp[0][1] = b[0]
     
    # Traverse the array A[] and B[]
    for i in range(1, n):
       
        # If A[i] is considered
        dp[i][0] = max(dp[i - 1][0],
                       dp[i - 1][1]) + a[i]
         
        # If B[i] is not considered
        dp[i][1] = max(dp[i - 1][0],
                       dp[i - 1][1])
         
        # If B[i] is considered
        if (i - 2 >= 0):
            dp[i][1] = max(dp[i][1], max(dp[i - 2][0],
                                         dp[i - 2][1]) + b[i])
        else:
             
            # If i = 1, then consider the
            # value of  dp[i][1] as b[i]
            dp[i][1] = max(dp[i][1], b[i])
 
    # Return maximum sum
    return max(dp[n - 1][0], dp[n - 1][1])
 
# Driver code
if __name__ == '__main__':
     
    # Given input
    A = [ 10, 1, 10, 10 ]
    B = [ 5, 50, 1, 5 ]
    N = len(A)
     
    # Function call
    print(MaximumSum(A, B, N))
     
# This code is contributed by MuskanKalra1


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the maximum sum
// that can be obtained from two given
// based on the following conditions
static int MaximumSum(int []a, int []b, int n)
{
   
    // Stores the maximum
    // sum from 0 to i
    int [,]dp = new int[n,2];
 
    // Initialize the value of
    // dp[0][0] and dp[0][1]
    dp[0,0] = a[0];
    dp[0,1] = b[0];
 
    // Traverse the array A[] and B[]
    for (int i = 1; i < n; i++)
    {
       
        // If A[i] is consiered
        dp[i,0] = Math.Max(dp[i - 1,0], dp[i - 1,1]) + a[i];
 
        // If B[i] is not considered
        dp[i,1] = Math.Max(dp[i - 1,0], dp[i - 1,1]);
 
        // If B[i] is considered
        if (i - 2 >= 0) {
            dp[i,1] = Math.Max(dp[i,1],
                           Math.Max(dp[i - 2,0],
                               dp[i - 2,1])
                               + b[i]);
        }
        else
        {
           
            // If i = 1, then consider the
            // value of  dp[i][1] as b[i]
            dp[i,1] = Math.Max(dp[i,1], b[i]);
        }
    }
 
    // Return maximum Sum
    return Math.Max(dp[n - 1,0], dp[n - 1,1]);
}
 
// Driver Code
public static void Main()
{
    // Given Input
    int []A = { 10, 1, 10, 10 };
    int []B = { 5, 50, 1, 5 };
    int N = A.Length;
 
    // Function Call
    Console.Write(MaximumSum(A, B, N));
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
70

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

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