-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3218.java
47 lines (45 loc) · 1.81 KB
/
_3218.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
package com.fishercoder.solutions.fourththousand;
import java.util.PriorityQueue;
public class _3218 {
public static class Solution1 {
/*
* My completely original solution.
*/
public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
PriorityQueue<Integer> maxHeapHorizontal = new PriorityQueue<>((a, b) -> b - a);
for (int cut : horizontalCut) {
maxHeapHorizontal.offer(cut);
}
PriorityQueue<Integer> maxHeapVertical = new PriorityQueue<>((a, b) -> b - a);
for (int cut : verticalCut) {
maxHeapVertical.offer(cut);
}
int verticalParts = 1;
int horizontalParts = 1;
int cost = 0;
while (!maxHeapHorizontal.isEmpty() || !maxHeapVertical.isEmpty()) {
Integer curr;
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++;
}
} else if (!maxHeapHorizontal.isEmpty()) {
curr = maxHeapHorizontal.poll();
cost += curr * verticalParts;
horizontalParts++;
} else {
curr = maxHeapVertical.poll();
cost += curr * horizontalParts;
verticalParts++;
}
}
return cost;
}
}
}