两个排序数组中的最大对数

发布于 2024-12-13 10:44:40 字数 619 浏览 1 评论 0原文

这不是一个原始问题。我遇到了一个复杂的问题,现在简化为以下问题:

有两个排序数组,AB,其中mn分别为 元素,m = \Theta(n)o(mn) 时间内运行的算法能否找到满足 A[i]-B[j] <= T 的最大对数,其中 T 是某个持续的?这怎么能做到呢?

编辑:

  1. 这些对应该是不相交的,即一个元素最多只能被选择一次。

  2. 算法应该在little-o(mn)内运行,这意味着在mn时间内运行的解决方案是不可接受的。

  3. 是否也可以找到我们选择的对?

说明:

如果数组是 a_1, a_2, ..., a_mb_1, b_2, ..., b_n,我需要找到对 (a_i, b_j) 使得 |a_i - b_j| <= T。不允许多次选择一个元素。我们怎样才能最大化给定数组的对的数量?

This is not an original problem. I had a complex problem which is now reduced to the following:

There are two sorted arrays, A and B with m and n elements respectively, m = \Theta(n)
Can an algorithm that runs in o(mn) time find the maximum number of pairs such that A[i]-B[j] <= T where T is some constant? How can this be done?

edit:

  1. The pairs should be disjoint, i.e. one element can be selected at most once.

  2. The algorithm should run in little-o(mn) meaning that a solution that runs in mn time is not acceptable.

  3. Is it also possible to find the pairs that we select?

Clarification:

If the arrays are a_1, a_2, ..., a_m and b_1, b_2, ..., b_n, I need to find pairs (a_i, b_j) such that |a_i - b_j| <= T. It is not allowed to choose an element more than once. How can we maximize the number of pairs given the arrays?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤慢歌 2024-12-20 10:44:40

更新2:

更新后的问题(仅使用任一数组中的元素一次,获取对,并且值的绝对差必须低于T)可能能够在O(n+m)时间内完成。我还没有充分考虑下面的算法来决定它是否总是获得最大数量的对,但在大多数情况下应该:

int i = 0;
int j = 0; 

while(i < A.length){
    while(j < B.length){
        if(A[i]-B[j] <= T){
            if(A[i]-B[j] >= -1 * T){
                addPair(i, j);
                j++;//don't consider this value the next time
            }
            break;
        } 
        j++;
    }
    i++;
}

UPDATE 2:

The updated question (only use an element from either array once, get the pairs, and the absolute difference of the values must be below T) might be able to be done in O(n+m) time. I haven't thought through the algorithm below enough to decide if it will always get the maximum number of pairs or not, but it should in most cases:

int i = 0;
int j = 0; 

while(i < A.length){
    while(j < B.length){
        if(A[i]-B[j] <= T){
            if(A[i]-B[j] >= -1 * T){
                addPair(i, j);
                j++;//don't consider this value the next time
            }
            break;
        } 
        j++;
    }
    i++;
}
故人的歌 2024-12-20 10:44:40

O(n lg n) = O(m lg m)中:根据A的元素创建平衡二叉搜索树,并在每个节点中存储a的索引元素和元素值。对于B的每个元素,搜索小于或等于B[j] + T的最大值。这个数字的索引会告诉你有多少个数字小于或等于这个数字。

In O(n lg n) = O(m lg m): Create a balanced binary search tree from the elements of A, and store in each node the index of an element together with the element value. For each element of B, search for the greatest value that is less than or equal to B[j] + T. The index of this number will tell you how many numbers are smaller than or equal to this number.

谁把谁当真 2024-12-20 10:44:40

如果您想要匹配|A[i]-B[j]|的对的数量<= T,其中每个 A[i]B[j] 在所有对中仅使用一次:

int lastB = 0;
int result=0;
for(int a = 0; a<A.size(); ++a) {
    const int minB = A[a] - T;  
    while(lastB<B.size() && B[lastB] < minB)
        ++lastB;
    const int maxB = A[a] + T;
    if (lastB<B.size() && B[lastB] > minB) {
        ++lastB;
        ++result;
    }
}
return result;

此算法扫描 中的最小范围>B,并确保 B 中没有元素被使用两次。

If you want the number of pairs matching|A[i]-B[j]| <= T, where each A[i] and B[j] are used only once in all pairs:

int lastB = 0;
int result=0;
for(int a = 0; a<A.size(); ++a) {
    const int minB = A[a] - T;  
    while(lastB<B.size() && B[lastB] < minB)
        ++lastB;
    const int maxB = A[a] + T;
    if (lastB<B.size() && B[lastB] > minB) {
        ++lastB;
        ++result;
    }
}
return result;

This algorithm scans minimal ranges in B, and makes sure that no element in B is used twice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文