通过从一个或两个中减少一个值来最小化操作以使两个数组相等
给定两个具有N个整数的数组A[]和B[] ,任务是找到使两个数组的所有元素相等所需的最小操作,在每个操作中,可以执行以下操作:
- 将A[i]的值减1 ,其中i位于[0, N)范围内。
- 将B[i]的值减1 ,其中i位于[0, N)范围内。
- 将A[i]和B[i]的值减1 ,其中i位于[0, N)范围内。
注意:数组A[]和B[]中的元素不必彼此相等。
例子:
Input: arr1[] = {1, 2, 3}, arr2[] = {5, 4, 3}
Output: 5
Explanation: Operations can be performed in the following way:
- Decrement element at index 2 of A[] by 1. Hence, A[] = {1, 2, 2}.
- Decrement element at index 2 of A[] by 1. Hence, A[] = {1, 2, 1}.
- Decrement element at index 0 of B[] by 1. Hence, B[] = {4, 4, 3}.
- Decrement element at index 0 of B[] by 1. Hence, B[] = {3, 4, 3}.
- Decrement element at index 1 of both A[] and B[] by 1. Hence A[] = {1, 1, 1} and B[] = {3, 3, 3}
Therefore, all the elements of both the arrays A[] and B[] can be made equal in 5 operation which is the minimum possible.
Input: A[] = {7, 2, 8, 5, 3}, B[] = {3, 4, 5, 9, 1}, N = 5
Output: 23
方法:给定的问题可以使用贪心方法来解决。由于所有可能的操作只会递减数组值,因此必须使所有元素都等于给定数组中的最小元素。假设min_A和min_B分别是数组A[]和B[]中的最小整数。因此,所需的答案将是[0, N)范围内i的所有可能值的max(A[i] – min_A, B[i] – min_B)之和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum operations
// required to make elements of each array
// equal of the given two arrays
int minOperations(int a[], int b[], int N)
{
// Stores the minimum element in array a[]
int min_a = *min_element(a, a + N);
// Stores the minimum element in array b[]
int min_b = *min_element(b, b + N);
// Variable to store the required ans
int ans = 0;
// Iterate over the elements
for (int i = 0; i < N; i++) {
// Store the difference between current
// element and minimum of respective array
int x = a[i] - min_a;
int y = b[i] - min_b;
// Add maximum of x and y to ans
ans += max(x, y);
}
// Return Answer
return ans;
}
// Driver Code
int main()
{
int a[] = { 7, 2, 8, 5, 3 };
int b[] = { 3, 4, 5, 9, 1 };
int N = sizeof(a) / sizeof(b[0]);
cout << minOperations(a, b, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the minimum operations
// required to make elements of each array
// equal of the given two arrays
static int minOperations(int []a, int []b, int N)
{
// Stores the minimum element in array a[]
int min_a = Arrays.stream(a).min().getAsInt();
// Stores the minimum element in array b[]
int min_b = Arrays.stream(b).min().getAsInt();
// Variable to store the required ans
int ans = 0;
// Iterate over the elements
for (int i = 0; i < N; i++) {
// Store the difference between current
// element and minimum of respective array
int x = a[i] - min_a;
int y = b[i] - min_b;
// Add maximum of x and y to ans
ans += Math.max(x, y);
}
// Return Answer
return ans;
}
// Driver Code
public static void main(String args[])
{
int []a = { 7, 2, 8, 5, 3 };
int []b = { 3, 4, 5, 9, 1 };
int N = a.length;
System.out.println(minOperations(a, b, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to find the minimum operations
# required to make elements of each array
# equal of the given two arrays
def minOperations(a, b, N):
# Stores the minimum element in array a[]
min_a = min(a)
# Stores the minimum element in array b[]
min_b = min(b)
# Variable to store the required ans
ans = 0
# Iterate over the elements
for i in range(N):
# Store the difference between current
# element and minimum of respective array
x = a[i] - min_a
y = b[i] - min_b
# Add maximum of x and y to ans
ans += max(x, y)
# Return Answer
return ans
# Driver Code
if __name__ == "__main__":
a = [7, 2, 8, 5, 3]
b = [3, 4, 5, 9, 1]
N = len(a)
print(minOperations(a, b, N))
# This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
using System.Linq;
public class GFG
{
// Function to find the minimum operations
// required to make elements of each array
// equal of the given two arrays
static int minOperations(int []a, int []b, int N)
{
// Stores the minimum element in array a[]
int min_a = a.Min();
// Stores the minimum element in array b[]
int min_b = b.Min();
// Variable to store the required ans
int ans = 0;
// Iterate over the elements
for (int i = 0; i < N; i++) {
// Store the difference between current
// element and minimum of respective array
int x = a[i] - min_a;
int y = b[i] - min_b;
// Add maximum of x and y to ans
ans += Math.Max(x, y);
}
// Return Answer
return ans;
}
// Driver Code
public static void Main()
{
int []a = { 7, 2, 8, 5, 3 };
int []b = { 3, 4, 5, 9, 1 };
int N = a.Length;
Console.Write(minOperations(a, b, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
23
时间复杂度: O(N)
辅助空间: O(1)