如何有效地配对袜子?

吃可爱长大的小学妹 提交于 2020-08-07 13:23:49

问题:

Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. 昨天我把干净的洗衣店的袜子配对,弄清楚我做的方式效率不高。 I was doing a naive search — picking one sock and "iterating" the pile in order to find its pair. 我正在做一个天真的搜索 - 挑选一个袜子并“迭代”堆,以找到它的对。 This requires iterating over n/2 * n/4 = n 2 /8 socks on average. 这需要迭代在n / 2 * N / 4 = N 2/8上平均的袜子。

As a computer scientist I was thinking what I could do? 作为一名计算机科学家,我在想我能做什么? Sorting (according to size/color/...) of course came to mind to achieve an O(NlogN) solution. 当然,为了实现O(NlogN)解决方案,我们会想到排序(根据大小/颜色/ ...)。

Hashing or other not-in-place solutions are not an option, because I am not able to duplicate my socks (though it could be nice if I could). 哈希或其他非就地解决方案不是一种选择,因为我无法复制我的袜子(尽管如果可能的话可能会很好)。

So, the question is basically: 所以,问题基本上是:

Given a pile of n pairs of socks, containing 2n elements (assume each sock has exactly one matching pair), what is the best way to pair them up efficiently with up to logarithmic extra space? 鉴于一堆n双袜子,包含2n元素(假设每个袜子只有一对匹配),有效配对多达对数额外空间的最佳方法是什么? (I believe I can remember that amount of info if needed.) (我相信如果需要的话我会记住那些信息。)

I will appreciate an answer that addresses the following aspects: 我将感谢一个解决以下方面的答案:

  • A general theoretical solution for a huge number of socks. 大量袜子的一般理论解决方案。
  • The actual number of socks is not that large, I don't believe my spouse and I have more than 30 pairs. 袜子的实际数量并不是那么大,我不相信我的配偶和我有超过30双。 (And it is fairly easy to distinguish between my socks and hers; can this be used as well?) (并且很容易区分我的袜子和她的袜子;这也可以使用吗?)
  • Is it equivalent to the element distinctness problem ? 它是否等同于元素清晰度问题

解决方案:

参考一: https://stackoom.com/question/yUED/如何有效地配对袜子
参考二: https://oldbug.net/q/yUED/How-to-pair-socks-from-a-pile-efficiently
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!