📌  相关文章
📜  通过最多与另一个数组交换K个元素来最大化数组总和

📅  最后修改于: 2021-05-17 23:23:37             🧑  作者: Mango

给定两个大小为N且整数K的数组AB ,任务是通过最多与数组B交换K个元素来找到数组A的最大可能和。

例子:

方法:

  1. 以非降序对数组A和B进行排序。
  2. 从头开始遍历数组A,从头开始遍历数组B,以便我们可以将数组A的最小元素与数组B的最大元素交换。
  3. 如果数组A的元素小于数组B的元素,请交换它们。否则,请打破循环。
  4. 最多对K个元素执行此操作。
  5. 求出结果数组A的总和。

下面是上述方法的实现。

C++
// C++ implementation to find maximum
// sum of array A by swapping
// at most K elements with array B
  
#include 
using namespace std;
  
// Function to find the maximum sum
void maximumSum(int a[], int b[],
                int k, int n)
{
    int i, j;
    sort(a, a + n);
    sort(b, b + n);
  
    // If element of array a is
    // smaller than that of
    // array b, swap them.
    for (i = 0, j = n - 1; i < k;
         i++, j--) {
        if (a[i] < b[j])
            swap(a[i], b[j]);
        else
            break;
    }
  
    // Find sum of resultant array
    int sum = 0;
    for (i = 0; i < n; i++)
        sum += a[i];
    cout << sum << endl;
}
  
int main()
{
    int K = 1;
    int A[] = { 2, 3, 4 };
    int B[] = { 6, 8, 5 };
  
    int N = sizeof(A) / sizeof(A[0]);
  
    maximumSum(A, B, K, N);
    return 0;
}


Java
// Java implementation to find maximum
// sum of array A by swapping
// at most K elements with array B
import java.util.*;
class GFG{
  
// Function to find the maximum sum
static void maximumSum(int a[], int b[],
                       int k, int n)
{
    int i, j;
    Arrays.sort(a);
    Arrays.sort(b);
  
    // If element of array a is
    // smaller than that of
    // array b, swap them.
    for (i = 0, j = n - 1; i < k; i++, j--)
    {
        if (a[i] < b[j])
        {
            int temp = a[i];
            a[i] = b[j];
            b[j] = temp;
        }
        else
            break;
    }
  
    // Find sum of resultant array
    int sum = 0;
    for (i = 0; i < n; i++)
        sum += a[i];
    System.out.print(sum +"\n");
}
  
// Driver Code
public static void main(String[] args)
{
    int K = 1;
    int A[] = { 2, 3, 4 };
    int B[] = { 6, 8, 5 };
  
    int N = A.length;
  
    maximumSum(A, B, K, N);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation to find maximum
# sum of array A by swapping
# at most K elements with array B
  
# Function to find the maximum sum
def maximumSum(a, b, k, n):
  
    a.sort()
    b.sort()
  
    # If element of array a is
    # smaller than that of
    # array b, swap them.
    i = 0
    j = n - 1
      
    while i < k:
        if (a[i] < b[j]):
            a[i], b[j] = b[j], a[i]
              
        else:
            break
              
        i += 1
        j -= 1
  
    # Find sum of resultant array
    sum = 0
    for i in range (n):
        sum += a[i]
          
    print(sum)
  
# Driver code
if __name__ == "__main__":
      
    K = 1
    A = [ 2, 3, 4 ]
    B = [ 6, 8, 5 ]
  
    N = len(A)
  
    maximumSum(A, B, K, N)
  
# This code is contributed by chitranayal


C#
// C# implementation to find maximum
// sum of array A by swapping
// at most K elements with array B
using System;
class GFG{
  
// Function to find the maximum sum
static void maximumSum(int []a, 
                       int []b,
                       int k, int n)
{
    int i, j;
    Array.Sort(a);
    Array.Sort(b);
  
    // If element of array a is
    // smaller than that of
    // array b, swap them.
    for (i = 0, j = n - 1; i < k; i++, j--)
    {
        if (a[i] < b[j])
        {
            int temp = a[i];
            a[i] = b[j];
            b[j] = temp;
        }
        else
            break;
    }
  
    // Find sum of resultant array
    int sum = 0;
    for (i = 0; i < n; i++)
        sum += a[i];
    Console.Write(sum +"\n");
}
  
// Driver Code
public static void Main()
{
    int K = 1;
    int []A = { 2, 3, 4 };
    int []B = { 6, 8, 5 };
  
    int N = A.Length;
  
    maximumSum(A, B, K, N);
}
}
  
// This code is contributed by Code_Mech


输出:
15

性能分析:

  • 时间复杂度: O(N * log N)
  • 辅助空间: O(1)