-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathSolution.java
46 lines (39 loc) · 1.56 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
// github.com/RodneyShag
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.ArrayList;
// Performance Improvements:
// - used BufferedReader instead of Scanner: https://www.hackerrank.com/challenges/countingsort4/forum/comments/223339
// - used StringBuffer to do 1 output to console instead of multiple outputs
public class Solution {
public static void main(String[] args) throws IOException {
final int maxValue = 100;
/* Create HashMap with empty "buckets" to put Strings into */
HashMap<Integer, ArrayList<String>> map = new HashMap(maxValue);
for (int i = 0; i < maxValue; i++) {
map.put(i, new ArrayList<String>());
}
/* Save input */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
String [] pair = br.readLine().split(" ");
int key = Integer.parseInt(pair[0]);
String value = (i < n/2) ? "-" : pair[1];
ArrayList<String> list = map.get(key);
list.add(value);
}
br.close();
/* Print output */
StringBuffer sb = new StringBuffer();
for (int i = 0; i < maxValue; i++) {
ArrayList<String> values = map.get(i);
for (String str : values) {
sb.append(str + " ");
}
}
System.out.println(sb);
}
}