-
Notifications
You must be signed in to change notification settings - Fork 692
/
Solution.java
55 lines (47 loc) · 1.87 KB
/
Solution.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
//Problem: https://www.hackerrank.com/challenges/countingsort4
//Java 8
/*
Initial Thoughts:
We can build a map that has Integer:Queue pairs
so that we don' mix up the order of the strings
resulting in a stable sort. Then all we need to
do is poll from the related queue each time we are printing
a number in sorted order
Time Complexity: O(n+k) //k is the total numbers and k is total unique numbers
Space Complexity: O(k) //we have to intialize an array to the number of unique numbers
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int[] frequencies = new int[100];
StringBuilder STDOUTT = new StringBuilder("");
Map<Integer,Queue<StringBuilder>> order = new HashMap<>();
for(int i = 0; i < n; i++)
{
String[] tmp = in.readLine().split(" ");
int num = Integer.parseInt(tmp[0]);
StringBuilder s = new StringBuilder(tmp[1]);
if(i < n/2) s = new StringBuilder("-");//use - as s for first half
//add the string to the queue associated with num
if(!order.containsKey(num))
{
Queue<StringBuilder> strs = new LinkedList();
order.put(num, strs);
}
order.get(num).add(s);
frequencies[num] = frequencies[num] + 1;
}
//For all sorted numbers
for(int i = 0; i < frequencies.length; i++)
{
for(int j = 0; j < frequencies[i]; j++)
{
STDOUTT.append(order.get(i).poll().toString() + " ");//Print every element from the queue
}
}
System.out.print(STDOUTT);
}
}