-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balanced File System Partitions
85 lines (63 loc) · 3.26 KB
/
Balanced File System Partitions
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
Balanced File System Partitions Solution:-
JAVA CODE:-
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class BalancedFileSystemPartitions {
static class Result {
public static int mostBalancedPartition(List<Integer> parent, List<Integer> files_size) {
if (parent.size() != files_size.size()) {
throw new RuntimeException("why!!??");
}
Map<Integer, Integer> indexToParent = IntStream.range(0, parent.size()).boxed()
.collect(Collectors.toMap(Function.identity(), parent::get));
Map<Integer, Integer> indexToFileSize = IntStream.range(0, files_size.size()).boxed()
.collect(Collectors.toMap(Function.identity(), files_size::get));
Map<Integer, List<Integer>> parentToIndexes = IntStream.range(0, parent.size()).boxed()
.collect(Collectors.groupingBy(parent::get));
Map<Integer, Integer> effectiveSize = fileSizeCompute(parentToIndexes, indexToFileSize);
int minDiff = Integer.MAX_VALUE;
for (Map.Entry<Integer, Integer> entry : indexToParent.entrySet()) {
int leftWt = effectiveSize.getOrDefault(entry.getKey(), 0);
int rightWt = effectiveSize.getOrDefault(entry.getValue(), 0) - leftWt;
minDiff = Math.min(Math.abs(leftWt - rightWt), minDiff);
}
return minDiff;
}
}
private static Map<Integer, Integer> fileSizeCompute(Map<Integer, List<Integer>> parentToIndexes,
Map<Integer, Integer> indexToFileSize) {
Map<Integer, Integer> effectiveSize = new HashMap<>();
LinkedHashMap<Integer, List<Integer>> sorted = parentToIndexes.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue, (a, b) -> b, LinkedHashMap::new));
for (Map.Entry<Integer, List<Integer>> p : sorted.entrySet()) {
for (Integer child : p.getValue()) {
effectiveSize.merge(p.getKey(),
indexToFileSize.getOrDefault(child, 0), Integer::sum);
}
}
return effectiveSize;
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int parentCount = Integer.parseInt(scanner.next().trim());
List<Integer> parent = IntStream.range(0, parentCount)
.mapToObj(i -> scanner.next().replaceAll("\\s+$", ""))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());
int files_sizeCount = Integer.parseInt(scanner.next().trim());
List<Integer> files_size = IntStream.range(0, files_sizeCount)
.mapToObj(i -> scanner.next().replaceAll("\\s+$", ""))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());
int result = Result.mostBalancedPartition(parent, files_size);
System.out.println(result);
scanner.close();
}
}