- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3219.java
47 lines (45 loc) · 1.81 KB
/
_3219.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
packagecom.fishercoder.solutions.fourththousand;
importjava.util.PriorityQueue;
publicclass_3219 {
publicstaticclassSolution1 {
/*
* My completely original solution.
*/
publiclongminimumCost(intm, intn, int[] horizontalCut, int[] verticalCut) {
PriorityQueue<Integer> maxHeapHorizontal = newPriorityQueue<>((a, b) -> b - a);
for (intcut : horizontalCut) {
maxHeapHorizontal.offer(cut);
}
PriorityQueue<Integer> maxHeapVertical = newPriorityQueue<>((a, b) -> b - a);
for (intcut : verticalCut) {
maxHeapVertical.offer(cut);
}
intverticalParts = 1;
inthorizontalParts = 1;
longcost = 0L;
while (!maxHeapHorizontal.isEmpty() || !maxHeapVertical.isEmpty()) {
Integercurr;
if (!maxHeapHorizontal.isEmpty() && !maxHeapVertical.isEmpty()) {
if (maxHeapHorizontal.peek() > maxHeapVertical.peek()) {
curr = maxHeapHorizontal.poll();
cost += curr * verticalParts;
horizontalParts++;
} else {
curr = maxHeapVertical.poll();
cost += curr * horizontalParts;
verticalParts++;
}
} elseif (!maxHeapHorizontal.isEmpty()) {
curr = maxHeapHorizontal.poll();
cost += curr * verticalParts;
horizontalParts++;
} else {
curr = maxHeapVertical.poll();
cost += curr * horizontalParts;
verticalParts++;
}
}
returncost;
}
}
}