Find the largest k numbers in k arrays stored across k machines

前端 未结 7 874
悲哀的现实
悲哀的现实 2020-12-28 19:34

This is an interview question. I have K machines each of which is connected to 1 central machine. Each of the K machines have an array of 4 byte numbers in file. You can use

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 19:58

    1) sort the items on every machine 2) use a k - binary heap on the central machine a) populate the heap with first (max) element from each machine b) extract the first element, and put back in the heap the first element from the machine that you extracted the element. (of course heapify your heap, after the element is added).

    Sort will be O(Nlog(N)) where N is the max array on the machines. O(k) - to build the heap O(klog(k)) to extract and populate the heap k times.

    Complexity is max(O(klog(k)),O(Nlog(N)))

提交回复
热议问题