-
Notifications
You must be signed in to change notification settings - Fork 22
/
[lint]. Merge k Sorted Arrays.java
executable file
·142 lines (117 loc) · 3.66 KB
/
[lint]. Merge k Sorted Arrays.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
M
tags: Heap, PriorityQueue, MinHeap
time: O(nlogk)
space: O(k)
Same as merge k sorted list, use priorityQueue
#### Priority Queue
- 由Merge k sorted list启发。用PriorityQueue,存那k个首发element
- PriorityQueue需要存储单位: 自己建一个Class Node 存val, x, y index.
- 因为array里没有 'next' pointer,只能存x,y来推next element
- Not sure why `new PriorityQueue<>(Comparator.comparing(a -> a.val));` is slower
```
/*
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 Expand
Heap Priority Queue
*/
//LintCode, return int[]
public class Solution {
public class Node {
int val, x, y;
public Node(int val, int x, int y) {
this.val = val;
this.x = x;
this.y = y;
}
}
public int[] mergekSortedArrays(int[][] arrays) {
List<Integer> rst = new ArrayList<>();
if (arrays == null || arrays.length == 0) return new int[0];
// Faster
// Somehow, slower: PriorityQueue<Node> queue = new PriorityQueue<>(Comparator.comparing(a -> a.val));
PriorityQueue<Node> queue = new PriorityQueue<>(arrays.length,
new Comparator<Node>() {
public int compare(Node a, Node b){
return a.val - b.val;
}
}
);
//init
for (int i = 0; i < arrays.length; i++) {
if (arrays[i].length != 0) {
queue.offer(new Node(arrays[i][0], i, 0));
}
}
while (!queue.isEmpty()) {
Node node = queue.poll();
rst.add(node.val);
if (node.y < arrays[node.x].length - 1) {
queue.offer(new Node(arrays[node.x][node.y + 1], node.x, node.y + 1));
}
}
int[] arrayResult = new int[rst.size()];
for (int i = 0; i < arrayResult.length; i++) {
arrayResult[i] = rst.get(i);
}
return arrayResult;
}
}
/*
LintCode, return list
Thoughts: 12.10.2015
Since we can't know the node's sibiling, as creating the prirority queue,
let's create a Node{val, x, y}
Then it's very similar to merge k sorted lists.
border case: arrays == null. length == 0 //[]return empty list.
*/
public class Solution {
public class Node {
int val, x, y;
public Node(int val, int x, int y) {
this.val = val;
this.x = x;
this.y = y;
}
}
public List<Integer> mergekSortedArrays(int[][] arrays) {
List<Integer> rst = new ArrayList<Integer>();
if (arrays == null || arrays.length == 0) {
return rst;
}
PriorityQueue<Node> queue = new PriorityQueue<Node>(arrays.length,
new Comparator<Node>() {
public int compare(Node a, Node b){
return a.val - b.val;
}
}
);
//init
for (int i = 0; i < arrays.length; i++) {
if (arrays[i].length != 0) {
queue.offer(new Node(arrays[i][0], i, 0));
}
}
Node node;
while (!queue.isEmpty()) {
node = queue.poll();
rst.add(node.val);
if (node.y < arrays[node.x].length - 1) {
queue.offer(new Node(arrays[node.x][node.y + 1], node.x, node.y + 1));
}
}
return rst;
}
}
```