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

📅  最后修改于: 2021-05-14 01:34:27             🧑  作者: Mango

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

例子:

方法:我们将使用动态编程来解决此问题。步骤如下:

  1. 创建N行两列的2D数组dp [] [] ,并将dp的所有元素初始化为infinity
  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


输出:
49

时间复杂度: O(N)