Skip to content

Commit b447e2d

Browse files
add 3218
1 parent 5e210ee commit b447e2d

File tree

2 files changed

+45
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src/main/java/com/fishercoder/solutions/fourththousand

2 files changed

+45
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3218 | [Minimum Cost for Cutting Cake I](https://leetcode.com/problems/minimum-cost-for-cutting-cake-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3218.java) | | Medium |
34
| 3217 | [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3217.java) | | Medium | LinkedList
45
| 3216 | [Lexicographically Smallest String After a Swap](https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3216.java) | | Easy | Greedy
56
| 3212 | [Count Submatrices With Equal Frequency of X and Y](https://leetcode.com/problems/count-submatrices-with-equal-frequency-of-x-and-y/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3212.java) | | Medium | Prefix Sum
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _3218 {
6+
public static class Solution1 {
7+
public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
8+
PriorityQueue<Integer> maxHeapHorizontal = new PriorityQueue<>((a, b) -> b - a);
9+
for (int cut : horizontalCut) {
10+
maxHeapHorizontal.offer(cut);
11+
}
12+
PriorityQueue<Integer> maxHeapVertical = new PriorityQueue<>((a, b) -> b - a);
13+
for (int cut : verticalCut) {
14+
maxHeapVertical.offer(cut);
15+
}
16+
int verticalParts = 1;
17+
int horizontalParts = 1;
18+
int cost = 0;
19+
while (!maxHeapHorizontal.isEmpty() || !maxHeapVertical.isEmpty()) {
20+
Integer curr;
21+
if (!maxHeapHorizontal.isEmpty() && !maxHeapVertical.isEmpty()) {
22+
if (maxHeapHorizontal.peek() > maxHeapVertical.peek()) {
23+
curr = maxHeapHorizontal.poll();
24+
cost += curr * verticalParts;
25+
horizontalParts++;
26+
} else {
27+
curr = maxHeapVertical.poll();
28+
cost += curr * horizontalParts;
29+
verticalParts++;
30+
}
31+
} else if (!maxHeapHorizontal.isEmpty()) {
32+
curr = maxHeapHorizontal.poll();
33+
cost += curr * verticalParts;
34+
horizontalParts++;
35+
} else {
36+
curr = maxHeapVertical.poll();
37+
cost += curr * horizontalParts;
38+
verticalParts++;
39+
}
40+
}
41+
return cost;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)