Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lists is an array of length K of sorted lists
- var next = new Array(k);
- var h = new Heap(); // h is sorted on the "value" key of each object you put in
- var result = new Array();
- for(var i = 0; i < lists.length; i++) {
- h.insert({value: lists[i][0], list: i});
- next[i] = 1;
- }
- while(!h.empty()) {
- var p = h.extractMin();
- result.append(p.value);
- if(next[p.list] < lists[p.list].length) {
- h.insert({value: lists[i][next[i]], list: i});
- next[p.list]++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement