总和至少为剩余数组元素的对子集的最小大小
给定两个由N个正整数组成的数组A[]和B[] ,任务是找到元素对(A[i], B[i])的子集的最小大小,使得所有元素的总和子集对至少是不包括在子集中的剩余数组元素A[]的总和,即(A[0] + B[0] + A[1] + B[1] + … + A[K – 1] + B[K – 1] >= A[K + 1] + … + A[N – 1] 。
例子:
Input: A[] = {3, 2, 2}, B[] = {2, 3, 1}
Output: 1
Explanation:
Choose the subset as {(3, 2)}. Now, the sum of subsets is 3 + 2 = 5, which greater than sum of remaining A[] = 3 + 1 = 4.
Input: A[] = {2, 2, 2, 2, 2}, B[] = {1, 1, 1, 1, 1}
Output: 3
朴素方法:最简单的方法是生成给定元素对的所有可能子集,并打印满足给定标准并具有最小大小的子集的大小。
时间复杂度: O(2 N )
辅助空间: O(1)
有效的方法:给定的问题可以通过使用贪心方法来解决,这个想法是使用排序并且可以观察到选择第i对作为结果子集的一部分,两个子集之间的差异减少了( 2*S[i] + U[i]) 。请按照以下步骤解决问题:
- 初始化一个数组,比如大小为N的差异 [] ,它存储每对元素的(2*A[i] + B[i])的值。
- 初始化一个变量,比如sum ,它跟踪剩余数组元素A[i]的总和。
- 初始化一个变量,比如K来跟踪结果子集的大小。
- 在[0, N)范围内迭代,并将差异 [i]的值更新为(2*A[i] + B[i])的值,并将sum的值减去A[i]的值。
- 以升序对给定的数组差异 []进行排序。
- 以相反的方式遍历数组difference[]直到sum为负,然后将difference[i]的值添加到sum中,并将K的值增加1 。
- 完成上述步骤后,打印出K的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum size
// of subset satisfying given criteria
void maximizeApples(vector& U, vector& S, int N)
{
// Stores the value of 2*S[i] + U[i]
vector x(N);
// Stores the difference between
// the 2 subsets
int sum = 0;
for (int i = 0; i < N; i++) {
// Update the value of sum
sum -= S[i];
x[i] += 2 * S[i] + U[i];
}
// Sort the array X[] in an
// ascending order
sort(x.begin(), x.end());
int ans = 0;
int j = N - 1;
// Traverse the array
while (sum <= 0) {
// Update the value of sum
sum += x[j--];
// Increment value of ans
ans++;
}
// Print the resultant ans
cout << ans;
}
// Driver Code
int main()
{
vector A = { 1, 1, 1, 1, 1 };
vector B = { 2, 2, 2, 2, 2 };
int N = A.size();
maximizeApples(A, B, N);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the minimum size
// of subset satisfying given criteria
private static void maximizeApples(
int[] U, int[] S, int N)
{
// Stores the value of 2*S[i] + U[i]
int[] x = new int[N];
// Stores the difference between
// the 2 subsets
int sum = 0;
for (int i = 0; i < N; i++) {
// Update the value of sum
sum -= S[i];
x[i] += 2 * S[i] + U[i];
}
// Sort the array X[] in an
// ascending order
Arrays.sort(x);
int ans = 0;
int j = N - 1;
// Traverse the array
while (sum <= 0) {
// Update the value of sum
sum += x[j--];
// Increment value of ans
ans++;
}
// Print the resultant ans
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int[] A = new int[] { 1, 1, 1, 1, 1 };
int[] B = new int[] { 2, 2, 2, 2, 2 };
int N = A.length;
maximizeApples(A, B, N);
}
}
Python3
# Python program for the above approach
# Function to find the minimum size
# of subset satisfying given criteria
def maximizeApples(U, S, N):
# Stores the value of 2*S[i] + U[i]
x = [0]*N
# Stores the difference between
# the 2 subsets
sum = 0
for i in range(N):
# Update the value of sum
sum -= S[i]
x[i] += 2 * S[i] + U[i]
# Sort the array X[] in an
# ascending order
x.sort()
ans = 0
j = N - 1
# Traverse the array
while sum <= 0:
# Update the value of sum
sum += x[j]
j = j - 1
# Increment value of ans
ans = ans + 1
# Print the resultant ans
print(ans)
# Driver Code
A = [1, 1, 1, 1, 1]
B = [2, 2, 2, 2, 2]
N = len(A)
maximizeApples(A, B, N)
# This code is contributed by Potta Lokesh
C#
// c# program for the above approach
using System.Collections.Generic;
using System;
class GFG
{
// Function to find the minimum size
// of subset satisfying given criteria
static void maximizeApples(int[] U, int[] S, int N)
{
// Stores the value of 2*S[i] + U[i]
List x = new List();
// Stores the difference between
// the 2 subsets
int sum = 0;
for (int i = 0; i < N; i++)
{
// Update the value of sum
sum -= S[i];
x.Add( 2 * S[i] + U[i]);
}
// Sort the array X[] in an
// ascending order
x.Sort();
int ans = 0;
int j = N - 1;
// Traverse the array
while (sum <= 0) {
// Update the value of sum
sum += x[j--];
// Increment value of ans
ans++;
}
// Print the resultant ans
Console.WriteLine(ans);
}
// Driver Code
public static void Main()
{
int[] A = new int[] { 1, 1, 1, 1, 1 };
int[] B = new int[] { 2, 2, 2, 2, 2 };
int N = A.Length;
maximizeApples(A, B, N);
}
}
// This code is contributed by amreshkumar3.
Javascript
3
时间复杂度: O(N*log N)
辅助空间: O(N)