给定两个长度分别为N和M 的数组arr1[]和arr2[] ,任务是找到对(i, j)的最大数量,使得2 * arr1[i] ≤ arr2[j] ( 1 ≤ i ≤ N和1 ≤ j ≤ M )。
注意:任何数组元素都可以是一对的一部分。
例子:
Input: N = 3, arr1[] = {3, 2, 1}, M = 4, arr2[] = {3, 4, 2, 1}
Output: 2
Explanation:
Only two pairs can be chosen:
- (1, 3): Choose elements arr1[3] and arr2[1].
Therefore, pair = (arr1[3], arr2[1]) = (1, 3). Also, 2 * arr1[3] ≤ arr2[1].
Now elements at positions 3 and 1 of arr1[] and arr2[] respectively, cannot be chosen. - (2, 2): Choose elements arr1[2] and arr2[2].
Therefore, pair = (arr1[2], arr2[2]) = (2, 4). Also, 2*arr1[2] <= arr2[2].
Now elements at position 2 of arr1[] and arr2[] cannot be chosen.
Input: N = 1, arr1[] = {40}, M = 4, arr2[] = {10, 20, 30, 40}
Output: 0
Explanation:
No Possible Pair exists which satisfies the condition.
朴素的方法:最简单的方法是首先对数组和每个元素arr1[i]进行排序,贪婪地找到给定数组arr2[] 中刚好大于或等于值2 * arr1[i]的元素,然后然后通过将所需对的总数增加1从arr2[] 中删除该元素。遍历整个数组arr1[] 后,打印对的数量。
时间复杂度: O(N * M),其中 N 和 M 是给定数组的长度。
辅助空间: O(N+M)
有效的方法:这个想法是通过在arr2[]中找到一个刚好大于或等于值2*arr1[i]其中0<=i<=N-1的元素来使用贪婪算法。请按照以下步骤解决问题:
- 对数组arr1[] 进行排序并初始化一个变量ans来存储最大对数。
- 在 Max Heap 中添加arr2[] 的所有元素。
- 以非递增顺序从i = (N – 1)到0遍历数组arr1[] 。
- 对于每个元素arr1[i] ,从最大堆中移除 peek 元素,直到2*arr1[i]变得小于或等于 peek 元素,如果找到这样的元素,则将ans增加1 。
- 遍历整个数组后,打印ans作为最大对数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the maximum
// number of required pairs
int numberOfPairs(int arr1[], int n,
int arr2[], int m)
{
// Max Heap to add values of arar2[]
priority_queue pq;
int i, j;
// Sort the array arr1[]
sort(arr1, arr1 + n);
// Push all arr2[] into Max Heap
for (j = 0; j < m; j++) {
pq.push(arr2[j]);
}
// Initialize the ans
int ans = 0;
// Traverse the arr1[] in
// decreasing order
for (i = n - 1; i >= 0; i--) {
// Remmove element until a
// required pair is found
if (pq.top() >= 2 * arr1[i]) {
ans++;
pq.pop();
}
}
// Return maximum number of pairs
return ans;
}
// Driver Code
int main()
{
// Given arrays
int arr1[] = { 3, 1, 2 };
int arr2[] = { 3, 4, 2, 1 };
int N = sizeof(arr1) / sizeof(arr1[0]);
int M = sizeof(arr2) / sizeof(arr2[0]);
// Function Call
cout << numberOfPairs(arr1, N,
arr2, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to return the maximum
// number of required pairs
static int numberOfPairs(int[] arr1, int n,
int[] arr2, int m)
{
// Max Heap to add values of arr2[]
PriorityQueue pQueue =
new PriorityQueue(
new Comparator()
{
public int compare(Integer lhs,
Integer rhs)
{
if (lhs < rhs)
{
return + 1;
}
if (lhs.equals(rhs))
{
return 0;
}
return -1;
}
});
int i, j;
// Sort the array arr1[]
Arrays.sort(arr1);
// Push all arr2[] into Max Heap
for(j = 0; j < m; j++)
{
pQueue.add(arr2[j]);
}
// Initialize the ans
int ans = 0;
// Traverse the arr1[] in
// decreasing order
for(i = n - 1; i >= 0; i--)
{
// Remove element until a
// required pair is found
if (pQueue.peek() >= 2 * arr1[i])
{
ans++;
pQueue.poll();
}
}
// Return maximum number of pairs
return ans;
}
// Driver Code
public static void main (String[] args)
{
// Given arrays
int[] arr1 = { 3, 1, 2 };
int[] arr2 = { 3, 4, 2, 1 };
int N = 3;
int M = 4;
// Function call
System.out.println(numberOfPairs(arr1, N,
arr2, M));
}
}
// This code is contributed by sallagondaavinashreddy7
Python3
# Python3 program for the above approach
# Function to return the maximum
# number of required pairs
def numberOfPairs(arr1, n, arr2, m):
# Max Heap to add values of arr2[]
pq = []
# Sort the array arr1[]
arr1.sort(reverse = False)
# Push all arr2[] into Max Heap
for j in range(m):
pq.append(arr2[j])
# Initialize the ans
ans = 2
# Traverse the arr1[] in
# decreasing order
i = n - 1
while (i >= 0):
# Remove element until a
# required pair is found
pq.sort(reverse = False)
if (pq[0] >= 2 * arr1[i]):
ans += 1
print(pq[0])
pq.remove(pq[0])
i -= 1
# Return maximum number of pairs
return ans
# Driver Code
if __name__ == '__main__':
# Given arrays
arr1 = [ 3, 2, 1 ]
arr2 = [ 3, 4, 2, 1 ]
N = len(arr1)
M = len(arr2)
# Function Call
print(numberOfPairs(arr1, N, arr2, M))
# This code is contributed by ipg2016107
2
时间复杂度: O(N*log N + M*log M),其中 N 和 M 是给定数组的长度。
辅助空间: O(N+M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。