forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeKSortedArrays.java
79 lines (74 loc) · 2.3 KB
/
MergeKSortedArrays.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
import java.util.*;
class MergeKSortedArrays
{
static class pair implements Comparable<pair>
{
int value;
int index;
@Override
public int compareTo(pair obj)
{
return this.value - obj.value;
}
}
public static void mergeKSortedArrays_util(LinkedHashMap<Integer, ArrayList<Integer>> hm, int n, int k)
{
// Create a min heap with k heap nodes. Every heap node has first element of an array
PriorityQueue<pair> minheap = new PriorityQueue<>();
for(int i=0; i<k; i++)
{
if(hm.get(i).size() == 0)
{
pair pr = new pair();
pr.value = Integer.MAX_VALUE;
pr.index = -1;
minheap.add(pr);
}
else
{
pair pr = new pair();
pr.value = hm.get(i).remove(0);
pr.index = i;
minheap.add(pr);
}
}
ArrayList<Integer> output = new ArrayList<>();
// Now one by one get the minimum element from min heap and replace it with next element of its array
while(output.size() != n*k)
{
pair temp = minheap.remove();
output.add(temp.value);
if(hm.get(temp.index).size() != 0)
{
pair pr = new pair();
pr.value = hm.get(temp.index).remove(0);
pr.index = temp.index;
minheap.add(pr);
}
/* pair pr = new pair();
pr.value = hm.get(i).remove(0);
pr.index = i;
minheap.add(pr);
p.add(new PQNode(Integer.MAX_VALUE, -1));
*/
}
for(Integer ele : output)
System.out.print(ele+" ");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int k = sc.nextInt(), n = sc.nextInt();
LinkedHashMap<Integer, ArrayList<Integer>> hm = new LinkedHashMap<Integer, ArrayList<Integer>>();
for(int i=0; i < k; i++)
{
hm.put(i,new ArrayList<Integer>());
for(int j=0; j < n; j++)
{
hm.get(i).add(sc.nextInt());
}
}
//MergeKSortedArrays me = MergeKSortedArrays();
mergeKSortedArrays_util(hm, n, k);
}
}