列表中具有重复项和位置的唯一对

Unique pairs in list with duplicates & position

本文关键字:位置 唯一 列表      更新时间:2023-10-16

在准备技术面试时,我的一个朋友遇到了一个有趣的问题:给定一个n个整数的列表,找出所有相同的整数对并返回它们的位置。

Example  input:  [3,6,6,6,1,3,1]  
Expected output: (0,5), (1,2), (1,3), (2,3), (4,6)

Stack Overflow有很多关于唯一对存在性检查或特殊情况(如无重复)的答案,但我没有找到一个通用的、快速的解决方案。我下面的方法的时间复杂度最好的情况是O(n),但最坏的情况是O(n^2)(输入值都相同)。

有没有办法把它降到O(n*logN)的最坏情况?

// output is a vector of pairs
using TVecPairs= vector<pair<size_t,size_t>>;
TVecPairs findPairs2( const vector<uint32_t> &input ) 
{
    // map keyvalue -> vector of indices
    unordered_map<uint32_t, vector<size_t>> mapBuckets;
    // stick into groups of same value
    for (size_t idx= 0; idx<input.size(); ++idx) {
        // append index for given key value
        mapBuckets[input[idx]].emplace_back( idx );
    }
    // list of index pairs
    TVecPairs out;
    // foreach group of same value
    for (const auto &kvp : mapBuckets) { 
        const vector<size_t> &group= kvp.second;
        for (auto itor= cbegin(group); itor!=cend(group); ++itor) {
            for (auto other= itor+1; other!=cend(group); ++other) {
                out.emplace_back( make_pair(*itor,*other) );
            }
        }
    }
    return out;
}

正如其他人所说,如果您想要以您提到的方式输出,则它是O(n^2)。如果你可以用另一种方式打印它,你可以在c++中用O(n*(从hashmap中插入/读取的复杂度)= O(n*log(n))来完成它。以下是描述上述内容的python代码:

def dupes(arrlist):
   mydict=dict()
   count = 0
   for x in arrlist:
      if mydict.has_key(x):
         mydict[x] = mydict[x] + [count]
      else:
         mydict[x] = [count]
      count = count + 1
   print mydict
   return

对于上面的例子:

>>> dupes([3, 6, 6, 6, 1, 3, 1])
{1: [4, 6], 3: [0, 5], 6: [1, 2, 3]}