n*****g 发帖数: 178 | 1 在网上看到这题在:Sort numbers stored on different machines
出处:http://www.geeksforgeeks.org/sort-numbers-stored-on-different-machines/
Given N machines. Each machine contains some numbers in sorted form. But the
amount of numbers, each machine has is not fixed. Output the numbers from
all the machine in sorted non-decreasing form.
Example:
Machine M1 contains 3 numbers: {30, 40, 50}
Machine M2 contains 2 numbers: {35, 45}
Machine M3 contains 5 numbers: {10, 60, 70, 80, 100}
Output: {10, 30, 35, 40, 45, 50, 60, 70, 80, 100}
这题说用min heap来做。不懂,有啥好处?我的理解三个指针就搞定了啊,各自指向链
表head,即:l1指向30,l2指向35,l3指向10。每次都拿三个出来比较,选出最小的,
然后指针往后推移。这不是简单明了吗,用heap什么意思?求指点! | C*7 发帖数: 234 | 2 区别就在于这3个指针比较的过程,如果换成10个machine,你打算如何比较这10个指针 | t***t 发帖数: 6066 | 3 this is typical external sorting.
you need a heap to sort the N heads. | s**x 发帖数: 7506 | 4 This works , I doubt this is the best one. Seems too much network
★ 发自iPhone App: ChineseWeb 8.7
【在 t***t 的大作中提到】 : this is typical external sorting. : you need a heap to sort the N heads.
| s***i 发帖数: 503 | 5 C++用priority_queue,Java用PriorityQueue,你不需要自己
实现min heap的,除非面试的人专门要考你怎么实现min heap,
那时候你再另外写一段code实现。 | t***t 发帖数: 6066 | 6 面试时min heap基本用现成的会fail。考到了都会要你写出来。
【在 s***i 的大作中提到】 : C++用priority_queue,Java用PriorityQueue,你不需要自己 : 实现min heap的,除非面试的人专门要考你怎么实现min heap, : 那时候你再另外写一段code实现。
|
|