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

前端 未结 7 827
悲哀的现实
悲哀的现实 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 20:13

    I would suggest something like this:

    • take the k largest numbers on each machine in sorted order O(Nk) where N is the number of element on each machine

    • sort each of these arrays of k elements by largest element (you will get k arrays of k elements sorted by largest element : a square matrix kxk)

    • take the "upper triangle" of the matrix made of these k arrays of k elements, (the k largest element will be in this upper triangle)

    • the central machine can now find the k largest element of these k(k+1)/2 elements

提交回复
热议问题