📌  相关文章
📜  通过从一个或两个中减少一个值来最小化操作以使两个数组相等

📅  最后修改于: 2022-05-13 01:56:08.942000             🧑  作者: Mango

通过从一个或两个中减少一个值来最小化操作以使两个数组相等

给定两个具有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[]中的元素不必彼此相等。

例子:

方法:给定的问题可以使用贪心方法来解决。由于所有可能的操作只会递减数组值,因此必须使所有元素都等于给定数组中的最小元素。假设min_Amin_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)