给定两个大小分别为N和M 的数组A[]和B[] ,其中每个元素都在[0, 9]范围内,任务是使数组A[] 的每个元素严格大于或小于每个元素数组B[] 中的元素通过将任一数组中的任何元素更改为[0, 9]范围内的任何数字,最少次数。
例子:
Input: A[] = [0, 1, 0], B[] = [2, 0, 0]
Output: 2
Explanation:
Modifying the array B[] to [2, 2, 2] makes every element in the array A[] strictly less than every element in the array B[].
Hence, the minimum number of changes required = 2.
Input: A[] = [0, 0, 1, 3, 3], B[] = [0, 2, 3]
Output: 3
Explanation:
Modifying the array B[] to [4, 4, 4] makes every element in the array A[] strictly less than every element in the array B[].
Hence, the minimum number of changes required = 3.
方法:解决给定问题的想法是使用两个大小为10 的辅助数组prefix_a[]和prefix_b[] ,其中prefix_a[i]和prefix_b[i]存储数组A[] ≤ i中元素的数量,并且数组B[] ≤ i 中的元素数分别为。请按照以下步骤解决问题:
- 使用{0}初始化大小为10 的两个数组prefix_a[]和prefix_b[] 。
- 将数组A[]和B[]中每个元素的频率分别存储在数组prefix_a和prefix_b 中。
- 通过使用变量i在范围[1, 9] 中迭代并更新prefix_a[i]为(prefix[i] + prefix_a[i – 1]) ,对数组prefix_a执行前缀和。
- 对数组prefix_b[]重复上述步骤。
- 在范围内迭代 [0, 9]使用变量i
- 存储操作数以使数组A[]中的每个元素严格大于数字i并使数组B[]中的每个元素都小于变量中的数字i ,例如X 。
- 用prefix_a[i] + M – prefix_b[i]初始化它。
- 类似地,存储操作数以使数组B[]中的每个元素严格大于数字i ,并使数组A[]中的每个元素都小于变量中的数字i ,例如Y。
- 用prefix_b[i] + N – prefix_a[i]初始化它。
- 将总体最小操作数更新为X和Y的最小值。将获得的最小值存储在一个变量中,比如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.
Javascript
3
时间复杂度: O(N + M)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live