How can I pair socks from a pile efficiently?

后端 未结 30 1192
旧巷少年郎
旧巷少年郎 2020-11-27 08:37

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

30条回答
  •  孤街浪徒
    2020-11-27 09:33

    Consider a hash-table of size 'N'.

    If we assume normal distribution, then the estimated number of 'insertions' to have atleast one sock mapped to one bucket is NlogN (ie, all buckets are full)

    I had derived this as a part of another puzzle,but I would be happy to be proven wrong. Here's my blog article on the same

    Let 'N' correspond to an approximate upper-bound on the number of number of unique colors/pattern of socks that you have.

    Once you have a collision(a.k.a : a match) simply remove that pair of socks. Repeat the same experiment with the next batch of NlogN socks. The beauty of it is that you could be making NlogN parallel comparisons(collision-resolution) because of the way the human mind works. :-)

提交回复
热议问题