Minimum Cost to cut a board into squares
Pattern: Huffman-style merges
Each cut costs its price times the number of pieces it crosses, and every cut in one direction adds a piece to the other direction. So make the most expensive cuts first, while they cross the fewest pieces: sort both lists from high to low and always take the dearer of the two next cuts.
The problem
A board is cut into unit squares. x lists the prices of the cuts in one direction and y in the other. A cut costs
its price times the number of pieces it runs across at that moment. Return the least total cost. x = [2, 1, 3, 1, 4],
y = [4, 1, 2] gives 42.
Think first
- What multiplies a cut's price?
- Does that multiplier grow or shrink over time?
- So when should the most expensive cuts happen?
Hints
Brute force, and why it fails
Try every order of the cuts: factorial.
The key insight
Delaying a cut can only increase its multiplier, so the dearest cuts must go first; ties can go either way.
Dry run
| total | 4 |
|---|---|
| xPieces | 2 |
| yPieces | 1 |
x cuts [4, 3, 2, 1, 1], y cuts [4, 2, 1]. Take x 4 (crosses 1 piece): 4.
The plan
sort x and y from high to low; xPieces = yPieces = 1
while cuts remain:
if the next x price >= the next y price: total += x * yPieces; xPieces += 1
else: total += y * xPieces; yPieces += 1
Complexity
- Time
O(m log m + n log n)- Space
O(m + n)
Two sorts and a merge.
Edge cases
- No cuts in one direction.
- Equal prices across the two lists.
- One cut.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public long minimumCostOfBreaking(int[] x, int[] y) {
Integer[] a = new Integer[x.length], b = new Integer[y.length];
for (int i = 0; i < x.length; i++) a[i] = x[i];
for (int i = 0; i < y.length; i++) b[i] = y[i];
Arrays.sort(a, Collections.reverseOrder()); // dearest cuts first, while they cross the fewest pieces
Arrays.sort(b, Collections.reverseOrder());
int i = 0, j = 0;
long xPieces = 1, yPieces = 1, total = 0;
while (i < a.length || j < b.length) {
if (j == b.length || (i < a.length && a[i] >= b[j])) {
total += a[i++] * yPieces; // an x cut runs across every y piece
xPieces++;
} else {
total += b[j++] * xPieces; // a y cut runs across every x piece
yPieces++;
}
}
return total;
}
}C++
class Solution {
public:
long long minimumCostOfBreaking(vector<int>& x, vector<int>& y) {
vector<int> a(x), b(y);
sort(a.rbegin(), a.rend()); // dearest cuts first, while they cross the fewest pieces
sort(b.rbegin(), b.rend());
size_t i = 0, j = 0;
long long xPieces = 1, yPieces = 1, total = 0;
while (i < a.size() || j < b.size()) {
if (j == b.size() || (i < a.size() && a[i] >= b[j])) {
total += a[i++] * yPieces; // an x cut runs across every y piece
xPieces++;
} else {
total += b[j++] * xPieces; // a y cut runs across every x piece
yPieces++;
}
}
return total;
}
};Python
class Solution:
def minimumCostOfBreaking(self, x: list[int], y: list[int]) -> int:
a, b = sorted(x, reverse=True), sorted(y, reverse=True) # dearest cuts first
i = j = 0
x_pieces = y_pieces = 1
total = 0
while i < len(a) or j < len(b):
if j == len(b) or (i < len(a) and a[i] >= b[j]):
total += a[i] * y_pieces # an x cut runs across every y piece
x_pieces += 1
i += 1
else:
total += b[j] * x_pieces # a y cut runs across every x piece
y_pieces += 1
j += 1
return totalCommon mistakes
- Multiplying by the pieces in the same direction.
- Sorting from low to high.