你会怎么样ñ排序,平均长度为K列出了为O(n *日志K)的时间?
how would you sort n sorted lists with average length K in O(n*log K) time?
推荐答案正如在评论你的问题时,O(n日志(K))是不可能的,但这里有几个上的this页面能够有效地完成任务;这里是:
As mentioned in the comments to your question, the O(nlog(k)) is not possible, but here are a couple of algorithms on this page that accomplish your task efficiently; here is one:
取每个列表的第一个元素 并创建一个堆(大小为k)。流行的 最小元素。发现从阵列 元素来了(比如说它来了 从列表编号i)。采取下一个 从列表我的元素,并推入 堆。对于走在了每个元素 合并后的名单,我们花了数(k)的时间。所以 时间复杂度为O(N *稳定常数),其中 N是元件的总数 所有在K表。
Take the first element of each list and create a heap (of size k). Pop the smallest element. Find the array from the element came (let's say it came from list number i). Take the next element from list i and push it in heap. For each element that go in the merged list, we spent log(k) time. So the time complexity is O(N*logk) where N is total number of the elements in all the K lists.
-written方式:阿布舍克戈亚尔
-written by: Abhishek Goyal
更多推荐
你会怎么排序ñ排序为O,平均长度K表(N *登录K)的时间?
发布评论