什么是加盟有序阵列的最佳方法是什么? [英] What is the best way to join ordered arrays?

查看:106
本文介绍了什么是加盟有序阵列的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数几个阵列。每个数组中的元素是有序的。数组没有重复的。

I have few arrays of integers. Elements in each array are ordered. Arrays does not have duplicates.

我要参加所有的数组为一个,这样的结果数组只包含每个数组中存在的元素。

I need to join all arrays into one so the resulting array contains only elements which exist in every array.

例如,我有数组

(1,2,3,4,5) (2,3,5) (1,2,4,5)

(1,2,3,4,5) (2,3,5) (1,2,4,5)

结果必须是(2,5)

什么是做到这一点,以达到最佳性能的最好方法是什么?

What is the best way to do it to achieve best performance?

推荐答案

如果该阵列应包含在所有的人都很多不同的号码,只有少数present,

If the arrays are expected to contain many different numbers and only few present in all of them,

  • 挑两个最小的阵列,通过他们的步进顺序(像一个合并),O(LEN1 + LEN2)计算它们的交集
  • 虽然不是所有的阵列已经被扫描,选择下一个最小的阵列,并计算其交叉口pviously处理数组的$ P $的交集,用按顺序进行扫描,如果长度(路口)*日志(长度(阵列))> =长度(阵列),望着路口的元素阵列,否则
  • pick the two smallest arrays, compute their intersection by stepping through them sequentially (like for a mergesort), O(len1 + len2)
  • while not all arrays have been scanned, pick the next smallest array, and compute its intersection with the intersection of the previously handled arrays, using a sequential scan if length(intersection)*log(length(array)) >= length(array), and looking up the elements of the intersection in array otherwise.

最坏情况复杂度为O(SUM(长度)),如果幸运的话,你身边 K *总和(日志(长)),获得其中<$ Ç$ C> K 是在交叉路口的元素数。

Worst case complexity is O(sum(lengths)), if you're lucky, you get around k * sum(log(length)), where k is the number of elements in the intersection.

这篇关于什么是加盟有序阵列的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆