📌  相关文章
📜  给定两个数组的最小总和

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

给定两个由N 个正整数组成的数组A[]B[]以及成本C 。我们可以从给定数组的每个索引中选择任何一个元素,即,对于任何索引i,我们只能选择元素A[i]B[i] 。任务是找到从给定的两个数组中选择N 个元素的最小总和,如果我们在下一次迭代中选择从A[]B[] 的任何元素,反之亦然,那么成本C也被添加到和。
注意:按索引升序选择元素,即0 ≤ i < N
例子:

方法:我们将使用动态规划来解决这个问题。以下是步骤:

  1. 创建N行两列的二维数组dp[][]并将 dp 的所有元素初始化为无穷大
  2. 从两个数组中添加元素可能有 4 种可能的情况:
    • 当先前添加的元素来自数组 a[] 时,添加来自数组 a[] 的元素。
    • 当先前添加的元素来自数组 b[] 时,从数组 a[] 添加一个元素。在这种情况下,将整数 C 与结果相加会带来惩罚。
    • 当先前添加的元素来自数组 b[] 时,添加数组 b[] 中的元素。
    • 当先前添加的元素来自数组 a[] 时,从数组 b[] 添加一个元素。在这种情况下,将整数 C 与结果相加会带来惩罚。
  3. 每次更新dp数组,取上述四个条件中的最小值。
  4. dp[n-1][0]dp[n-1][1]的最小值是选择N 个元素的最小总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that prints minimum sum
// after selecting N elements
void minimumSum(int a[], int b[],
                int c, int n)
{
 
    // Initialise the dp array
    vector > dp(n,
                            vector(2,
                                        1e6));
 
    // Base Case
    dp[0][0] = a[0];
    dp[0][1] = b[0];
 
    for (int i = 1; i < n; i++) {
 
        // Adding the element of array a if
        // previous element is also from array a
        dp[i][0] = min(dp[i][0],
                    dp[i - 1][0] + a[i]);
 
        // Adding the element of array a if
        // previous element is from array b
        dp[i][0] = min(dp[i][0],
                    dp[i - 1][1] + a[i] + c);
 
        // Adding the element of array b if
        // previous element is from array a
        // with an extra penalty of integer C
        dp[i][1] = min(dp[i][1],
                    dp[i - 1][0] + b[i] + c);
 
        // Adding the element of array b if
        // previous element is also from array b
        dp[i][1] = min(dp[i][1],
                    dp[i - 1][1] + b[i]);
    }
 
    // Print the minimum sum
    cout << min(dp[n - 1][0],
                dp[n - 1][1])
        << "\n";
}
 
// Driver Code
int main()
{
    // Given array arr1[] and arr2[]
    int arr1[] = { 7, 6, 18, 6, 16,
                18, 1, 17, 17 };
 
    int arr2[] = { 6, 9, 3, 10, 9,
                1, 10, 1, 5 };
 
    // Given cost
    int C = 2;
 
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Function Call
    minimumSum(arr1, arr2, C, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function that prints minimum sum
// after selecting N elements
static void minimumSum(int a[], int b[],
                       int c, int n)
{
     
    // Initialise the dp array
    int [][]dp = new int[n][2];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            dp[i][j] = (int) 1e6;
        }
    }
     
    // Base Case
    dp[0][0] = a[0];
    dp[0][1] = b[0];
 
    for(int i = 1; i < n; i++)
    {
         
        // Adding the element of array a if
        // previous element is also from array a
        dp[i][0] = Math.min(dp[i][0],
                            dp[i - 1][0] + a[i]);
 
        // Adding the element of array a if
        // previous element is from array b
        dp[i][0] = Math.min(dp[i][0],
                            dp[i - 1][1] + a[i] + c);
 
        // Adding the element of array b if
        // previous element is from array a
        // with an extra penalty of integer C
        dp[i][1] = Math.min(dp[i][1],
                            dp[i - 1][0] + b[i] + c);
 
        // Adding the element of array b if
        // previous element is also from array b
        dp[i][1] = Math.min(dp[i][1],
                            dp[i - 1][1] + b[i]);
    }
 
    // Print the minimum sum
    System.out.print(Math.min(dp[n - 1][0],
                              dp[n - 1][1]) + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr1[] and arr2[]
    int arr1[] = { 7, 6, 18, 6, 16,
                   18, 1, 17, 17 };
 
    int arr2[] = { 6, 9, 3, 10, 9,
                   1, 10, 1, 5 };
 
    // Given cost
    int C = 2;
 
    int N = arr1.length;
 
    // Function call
    minimumSum(arr1, arr2, C, N);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function that prints minimum sum
# after selecting N elements
def minimumSum(a, b, c, n):
     
    # Initialise the dp array
    dp = [[1e6 for i in range(2)]
            for j in range(n)]
 
    # Base Case
    dp[0][0] = a[0]
    dp[0][1] = b[0]
 
    for i in range(1, n):
 
        # Adding the element of array a if
        # previous element is also from array a
        dp[i][0] = min(dp[i][0],
                    dp[i - 1][0] + a[i])
 
        # Adding the element of array a if
        # previous element is from array b
        dp[i][0] = min(dp[i][0],
                    dp[i - 1][1] + a[i] + c)
 
        # Adding the element of array b if
        # previous element is from array a
        # with an extra penalty of integer C
        dp[i][1] = min(dp[i][1],
                    dp[i - 1][0] + b[i] + c)
 
        # Adding the element of array b if
        # previous element is also from array b
        dp[i][1] = min(dp[i][1],
                    dp[i - 1][1] + b[i])
 
    # Print the minimum sum
    print(min(dp[n - 1][0], dp[n - 1][1]))
 
# Driver code
if __name__ == '__main__':
 
    # Given array arr[]
    arr1 = [ 7, 6, 18, 6, 16,
            18, 1, 17, 17 ]
 
    arr2 = [ 6, 9, 3, 10, 9,
            1, 10, 1, 5 ]
 
    # Given cost
    C = 2
 
    N = len(arr1)
 
    # Function Call
    minimumSum(arr1, arr2, C, N)
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function that prints minimum sum
// after selecting N elements
static void minimumSum(int []a, int []b,
                       int c, int n)
{
     
    // Initialise the dp array
    int [,]dp = new int[n, 2];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            dp[i, j] = (int)1e6;
        }
    }
     
    // Base Case
    dp[0, 0] = a[0];
    dp[0, 1] = b[0];
 
    for(int i = 1; i < n; i++)
    {
         
        // Adding the element of array a if
        // previous element is also from array a
        dp[i, 0] = Math.Min(dp[i, 0],
                            dp[i - 1, 0] + a[i]);
 
        // Adding the element of array a if
        // previous element is from array b
        dp[i, 0] = Math.Min(dp[i, 0],
                            dp[i - 1, 1] + a[i] + c);
 
        // Adding the element of array b if
        // previous element is from array a
        // with an extra penalty of integer C
        dp[i, 1] = Math.Min(dp[i, 1],
                            dp[i - 1, 0] + b[i] + c);
 
        // Adding the element of array b if
        // previous element is also from array b
        dp[i, 1] = Math.Min(dp[i, 1],
                            dp[i - 1, 1] + b[i]);
    }
 
    // Print the minimum sum
    Console.Write(Math.Min(dp[n - 1, 0],
                           dp[n - 1, 1]) + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array arr1[] and arr2[]
    int []arr1 = { 7, 6, 18, 6, 16,
                   18, 1, 17, 17 };
 
    int []arr2 = { 6, 9, 3, 10, 9,
                   1, 10, 1, 5 };
 
    // Given cost
    int C = 2;
 
    int N = arr1.Length;
 
    // Function call
    minimumSum(arr1, arr2, C, N);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

49

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live