-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathNetwork-Cabling.java
40 lines (36 loc) · 1.31 KB
/
Network-Cabling.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
import java.util.*;
import static java.lang.Math.abs;
// https://www.codingame.com/ide/puzzle/network-cabling
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
TreeMap<Integer, ArrayList<Integer>> treeMap = new TreeMap<>(); //for every X there is an array of Y.
for (int i = 0; i < N; i++) {
int X = in.nextInt();
if (!treeMap.containsKey(X)) { //guard of override with new ArrayList<>
treeMap.put(X,new ArrayList<>());
}
int Y = in.nextInt();
treeMap.get(X).add(Y);
}
ArrayList<Integer> yArray = new ArrayList<>();
for (ArrayList<Integer> yValue : treeMap.values()) {
yArray.addAll(yValue);
}
Collections.sort(yArray); //Sorted array of all Y's for finding the median.
int median;
if (yArray.size() % 2 != 0) {
median = yArray.get(yArray.size() / 2);
} else {
median = (yArray.get(yArray.size() / 2) + yArray.get((yArray.size() / 2) - 1)) / 2;
}
long count = abs(treeMap.lastKey() - treeMap.firstKey()); //The main cable
for (ArrayList<Integer> treeY : treeMap.values()) { //The cables structure using the median for minimum length.
for (Integer y : treeY) {
count += abs(median-y);
}
}
System.out.println(count);
}
}