给定两个由N和M 个整数组成的数组A[]和B[] ,任务是找到使数组A[]的最小元素至少是数组B[]的最大元素所需的最少操作次数这样在每次操作中,任何数组元素A[]都可以增加1或任何数组元素B[]可以减少1 。
例子:
Input: A[] = {2, 3}, B[] = {3, 5}
Output: 3
Explanation:
Following are the operations performed:
- Increase the value of A[1] by 1 modifies the array A[] = {3, 3}.
- Decrease the value of B[2] by 1 modifies the array B[] = {3, 4}.
- Decrease the value of B[2] by 1 modifies the array B[] = {3, 3}.
After the above operations, the minimum elements of the array A[] is 3 which is greater than or equal to the maximum element of the array B[] is 3. Therefore, the total number of operations is 3.
Input: A[] = {1, 2, 3}, B[] = {4}
Output: 4
方法:该问题可以通过使用贪婪方法来解决。请按照以下步骤解决给定的问题:
- 按升序对数组A[]进行排序。
- 按降序对数组B[]进行排序。
- 遍历两个数组,而 A[i] < B[i] ,以使数组A[]和B[] 的所有元素,直到索引i相等,例如x ,然后给出操作总数经过:
=> (B[0] + B[1] + … + B[i]) – i*x + (A[0] + A[1] + … + A[i]) + i*x
=> (B[0] – A[0]) + (B[1] – A[1]) + … + (B[i] – A[i]).
- 遍历两个数组,直到A[i] 的值小于B[i] ,并且(B[i] – A[i])的值小于变量,例如ans 。
- 完成上述步骤后,打印ans的值作为所需的最小操作次数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define ll long long
// Comparator function
bool cmp(ll a, ll b) { return a > b; }
// Function to find the minimum number
// of operation required to satisfy the
// given conditions
int FindMinMoves(vector A, vector B)
{
int n, m;
n = A.size();
m = B.size();
// sort the array A and B in the
// ascending and descending order
sort(A.begin(), A.end());
sort(B.begin(), B.end(), cmp);
ll ans = 0;
// Iterate over both the arrays
for (int i = 0; i < min(m, n)
&& A[i] < B[i];
++i) {
// Add the difference to the
// variable answer
ans += (B[i] - A[i]);
}
// Return the resultant operations
return ans;
}
// Driver Code
int main()
{
vector A = { 2, 3 };
vector B = { 3, 5 };
cout << FindMinMoves(A, B);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
class GFG{
// Comparator function
public static boolean cmp(int a, int b)
{
return a > b;
}
// Function to find the minimum number
// of operation required to satisfy the
// given conditions
public static int FindMinMoves(int[] A, int[] B)
{
int n, m;
n = A.length;
m = B.length;
// Sort the array A and B in the
// ascending and descending order
Arrays.sort(A);
Arrays.sort(B);
int ans = 0;
// Iterate over both the arrays
for(int i = 0;
i < Math.min(m, n) && A[i] < B[i]; ++i)
{
// Add the difference to the
// variable answer
ans += (B[i] - A[i]);
}
// Return the resultant operations
return ans;
}
// Driver Code
public static void main(String args[])
{
int[] A = { 2, 3 };
int[] B = { 3, 5 };
System.out.println(FindMinMoves(A, B));
}
}
// This code is contributed by _saurabh_jaiswal
Python3
# Python3 program for the above approach
# Function to find the minimum number
# of operation required to satisfy the
# given conditions
def FindMinMoves(A, B):
n = len(A)
m = len(B)
# sort the array A and B in the
# ascending and descending order
A.sort()
B.sort(reverse = True)
ans = 0
# Iterate over both the arrays
i = 0
for i in range(min(m, n) and A[i] < B[i]):
# Add the difference to the
# variable answer
ans += (B[i] - A[i])
# Return the resultant operations
return ans
# Driver Code
A = [ 2, 3 ]
B = [ 3, 5 ]
print(FindMinMoves(A, B))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG{
// Comparator function
public static bool cmp(int a, int b)
{
return a > b;
}
// Function to find the minimum number
// of operation required to satisfy the
// given conditions
public static int FindMinMoves(int[] A, int[] B)
{
int n, m;
n = A.Length;
m = B.Length;
// Sort the array A and B in the
// ascending and descending order
Array.Sort(A);
Array.Sort(B);
int ans = 0;
// Iterate over both the arrays
for(int i = 0;
i < Math.Min(m, n) && A[i] < B[i]; ++i)
{
// Add the difference to the
// variable answer
ans += (B[i] - A[i]);
}
// Return the resultant operations
return ans;
}
// Driver Code
public static void Main()
{
int[] A = { 2, 3 };
int[] B = { 3, 5 };
Console.Write(FindMinMoves(A, B));
}
}
// This code is contributed by target_2.
Javascript
3
时间复杂度: O(K*log K),其中 K 的值为 max(N, M)。
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。