📌  相关文章
📜  尽量减少替换,以使数组中的每个元素超过另一个给定数组中的每个元素

📅  最后修改于: 2021-04-17 14:22:12             🧑  作者: Mango

给定分别为NM的两个数组A []B [] ,其中每个元素的范围为[0,9] ,任务是使数组A []的每个元素严格大于或小于每个通过将任意元素从任一数组更改为[0,9]范围内的任何数字(最小次数)来更改数组B []中的元素。

例子:

方法:解决给定问题的想法是使用大小为10的两个辅助数组prefix_a []prefix_b [] ,其中prefix_a [i]prefix_b [i]存储数组A [] ≤i的元素数,并且数组B []≤i中元素的数量。请按照以下步骤解决问题:

  • 使用{0}初始化大小为10的两个数组prefix_a []prefix_b []
  • 将每个元素的频率分别存储在数组prefix_aprefix_b中的数组A []B []中。
  • 通过使用变量i在范围[1,9]中进行迭代,对数组prefix_a执行前缀求和,并将prefix_a [i]更新为(prefix [i] + prefix_a [i – 1])
  • 对数组prefix_b []重复上述步骤。
  • 在范围内迭代 [0,9]使用变量i
    • 存储操作的数目使每个元件阵列A []严格小于数字更大,使每个元素在数组B []小于位数i的一个变量,表示X。
    • 使用prefix_a [i] + M – prefix_b [i]对其进行初始化。
    • 类似地,存储操作的数目使每个元件在数组B []严格大于数字更大,并且使每一个元件在数组A []小于位数i的一个变量,表示Y.
    • prefix_b [i] + N – prefix_a [i]初始化它。
    • 将总的最小操作总数更新为XY的最小值。将获得的最小值存储在变量中,例如ans
  • 完成上述步骤后,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimize
// replacements to make every element
// in the array A[] strictly greater
// than every element in B[] or vice-versa
void MinTime(int* a, int* b, int n, int m)
{
 
    // Store the final result
    int ans = INT_MAX;
 
    // Create two arrays and
    // initialize with 0s
    int prefix_a[10] = { 0 };
    int prefix_b[10] = { 0 };
 
    // Traverse the array a[]
    for (int i = 0; i < n; i++) {
 
        // Increment prefix_a[a[i]] by 1
        prefix_a[a[i]]++;
    }
 
    // Traverse the array b[]
    for (int i = 0; i < m; i++) {
 
        // Increment prefix_b[b[i]] by 1
        prefix_b[b[i]]++;
    }
 
    // Calculate prefix sum
    // of the array a[]
    for (int i = 1; i <= 9; i++) {
        prefix_a[i] += prefix_a[i - 1];
    }
 
    // Calculate prefix sum
    // of the array b[]
    for (int i = 1; i <= 9; i++) {
        prefix_b[i] += prefix_b[i - 1];
    }
 
    // Iterate over the range [0, 9]
    for (int i = 0; i <= 9; i++) {
 
        // Make every element in array
        // a[] strictly greater than digit
        // i and make every element in the
        // array b[] less than digit i
        ans = min(ans, prefix_a[i] + m
                           - prefix_b[i]);
 
        // Make every element in array
        // b[] strictly greater than digit
        // i and make every element in
        // array a[] less than digit i
        ans = min(ans, n - prefix_a[i]
                           + prefix_b[i]);
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int A[] = { 0, 0, 1, 3, 3 };
    int B[] = { 2, 0, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
 
    MinTime(A, B, N, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
  
 // Function to find the minimize
// replacements to make every element
// in the array A[] strictly greater
// than every element in B[] or vice-versa
static void MinTime(int []a, int []b, int n, int m)
{
 
    // Store the final result
    int ans = 2147483647;
 
    // Create two arrays and
    // initialize with 0s
    int []prefix_a = new int[10];
    int []prefix_b = new int[10];
    
    // Traverse the array a[]
    for (int i = 0; i < n; i++) {
 
        // Increment prefix_a[a[i]] by 1
        prefix_a[a[i]]++;
    }
 
    // Traverse the array b[]
    for (int i = 0; i < m; i++) {
 
        // Increment prefix_b[b[i]] by 1
        prefix_b[b[i]]++;
    }
 
    // Calculate prefix sum
    // of the array a[]
    for (int i = 1; i <= 9; i++) {
        prefix_a[i] += prefix_a[i - 1];
    }
 
    // Calculate prefix sum
    // of the array b[]
    for (int i = 1; i <= 9; i++) {
        prefix_b[i] += prefix_b[i - 1];
    }
 
    // Iterate over the range [0, 9]
    for (int i = 0; i <= 9; i++) {
 
        // Make every element in array
        // a[] strictly greater than digit
        // i and make every element in the
        // array b[] less than digit i
        ans = Math.min(ans, prefix_a[i] + m
                           - prefix_b[i]);
 
        // Make every element in array
        // b[] strictly greater than digit
        // i and make every element in
        // array a[] less than digit i
        ans = Math.min(ans, n - prefix_a[i]
                           + prefix_b[i]);
    }
 
    // Print the answer
    System.out.println(ans);
}
 
// Driver Code
public static void main(String [] args)
{
    int []A = { 0, 0, 1, 3, 3 };
    int []B = { 2, 0, 3 };
    int N = A.length;
    int M = B.length;
 
    MinTime(A, B, N, M);
}
}
 
// This code is contributed by chitranayal.


Python3
# Python program for the above approach
 
# Function to find the minimize
# replacements to make every element
# in the array A[] strictly greater
# than every element in B[] or vice-versa
def MinTime(a, b, n, m):
   
    # Store the final result
    ans = float('inf')
 
    # Create two arrays and
    # initialize with 0s
    prefix_a = [ 0 ]*10
    prefix_b = [ 0 ]*10
 
    # Traverse the array a[]
    for i in range(n):
       
        # Increment prefix_a[a[i]] by 1
        prefix_a[a[i]] += 1
 
    # Traverse the array b[]
    for i in range(m):
       
        # Increment prefix_b[b[i]] by 1
        prefix_b[b[i]] += 1
 
    # Calculate prefix sum
    # of the array a[]
    for i in range(1, 10):
        prefix_a[i] += prefix_a[i - 1]
 
    # Calculate prefix sum
    # of the array b[]
    for i in range(1, 10):
        prefix_b[i] += prefix_b[i - 1]
 
    # Iterate over the range [0, 9]
    for i in range(1, 10):
 
        # Make every element in array
        # a[] strictly greater than digit
        # i and make every element in the
        # array b[] less than digit i
        ans = min(ans, prefix_a[i] + m- prefix_b[i])
 
        # Make every element in array
        # b[] strictly greater than digit
        # i and make every element in
        # array a[] less than digit i
        ans = min(ans, n - prefix_a[i] + prefix_b[i])
 
    # Print the answer
    print(ans)
 
# Driver Code
A = [ 0, 0, 1, 3, 3 ]
B = [ 2, 0, 3 ]
N = len(A)
M = len(B)
MinTime(A, B, N, M)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
  
 // Function to find the minimize
// replacements to make every element
// in the array A[] strictly greater
// than every element in B[] or vice-versa
static void MinTime(int []a, int []b, int n, int m)
{
 
    // Store the final result
    int ans = 2147483647;
 
    // Create two arrays and
    // initialize with 0s
    int []prefix_a = new int[10];
    int []prefix_b = new int[10];
    Array.Clear(prefix_a,0,prefix_a.Length);
    Array.Clear(prefix_b,0,prefix_b.Length);
 
    // Traverse the array a[]
    for (int i = 0; i < n; i++) {
 
        // Increment prefix_a[a[i]] by 1
        prefix_a[a[i]]++;
    }
 
    // Traverse the array b[]
    for (int i = 0; i < m; i++) {
 
        // Increment prefix_b[b[i]] by 1
        prefix_b[b[i]]++;
    }
 
    // Calculate prefix sum
    // of the array a[]
    for (int i = 1; i <= 9; i++) {
        prefix_a[i] += prefix_a[i - 1];
    }
 
    // Calculate prefix sum
    // of the array b[]
    for (int i = 1; i <= 9; i++) {
        prefix_b[i] += prefix_b[i - 1];
    }
 
    // Iterate over the range [0, 9]
    for (int i = 0; i <= 9; i++) {
 
        // Make every element in array
        // a[] strictly greater than digit
        // i and make every element in the
        // array b[] less than digit i
        ans = Math.Min(ans, prefix_a[i] + m
                           - prefix_b[i]);
 
        // Make every element in array
        // b[] strictly greater than digit
        // i and make every element in
        // array a[] less than digit i
        ans = Math.Min(ans, n - prefix_a[i]
                           + prefix_b[i]);
    }
 
    // Print the answer
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main()
{
    int []A = { 0, 0, 1, 3, 3 };
    int []B = { 2, 0, 3 };
    int N = A.Length;
    int M = B.Length;
 
    MinTime(A, B, N, M);
}
}
 
// This code is contributed by bgangwar59.


输出:
3

时间复杂度: O(N + M)
辅助空间: O(1)