-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMergeKSortedArrays.java
More file actions
78 lines (63 loc) · 1.56 KB
/
MergeKSortedArrays.java
File metadata and controls
78 lines (63 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Merge k Sorted Arrays
Given k sorted integer arrays, merge them into one sorted array.
Example
Given 3 sorted arrays:
[
[1, 3, 5, 7],
[2, 4, 6],
[0, 8, 9, 10, 11]
]
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].
Challenge
Do it in O(N log k).
•N is the total number of integers.
•k is the number of arrays.
Tags Heap Priority Queue
*/
import java.util.PriorityQueue;
class Element {
public int row, val, col;
Element(int row, int col, int val){
this.row = row;
this.col = col;
this.val = val;
}
}
public class Solution {
private Comparator<Element> TestComparator = new Comparator<Element>(){
public int compare(Element left, Element right){
return left.val - right.val;
}
};
/**
* @param arrays k sorted integer arrays
* @return a sorted array
*/
public int[] mergekSortedArrays(int[][] arrays) {
if (arrays == null) {
return new int[0];
}
Queue<Element> heap = new PriorityQueue<Element>(arrays.length, TestComparator);
int total_size = 0;
for (int i = 0; i < arrays.length; i++) {
if (arrays[i].length != 0) {
Element tmp = new Element(i,0,arrays[i][0]);
heap.add(tmp);
total_size += arrays[i].length;
}
}
int[] result = new int[total_size];
int index = 0;
while (!heap.isEmpty()) {
Elment tmp = heap.poll();
result[index++] = tmp.val;
if (tmp.col + 1 < arrays[tmp.row].length) {
tmp.col++;
tmp.val = arrays[tmp.row][tmp.col];
heap.add(tmp);
}
}
return result;
}
}