13. Minimum Cost to Cut a Board into Squares
β GFG solution to the Minimum Cost to Cut a Board into Squares problem: find optimal cutting strategy using greedy approach with sorting. π
Note: The solution for this problem is being published today as I was engaged in a hackathon yesterday. Thank you for your understanding.
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a board of dimensions n Γ m
that needs to be cut into n*m
squares. The cost of making a cut along a horizontal or vertical edge is provided in two arrays:
x[]
: Cutting costs along the vertical edges (length-wise).y[]
: Cutting costs along the horizontal edges (width-wise).
Find the minimum total cost required to cut the board into squares optimally.
π Examples
Example 1
Input: n = 4, m = 6, x[] = [2, 1, 3, 1, 4], y[] = [4, 1, 2]
Output: 42
Explanation:
Initially no. of horizontal segments = 1 & no. of vertical segments = 1.
Optimal way to cut into square is:
β’ Pick 4 (from x) -> vertical cut, Cost = 4 Γ horizontal segments = 4,
Now, horizontal segments = 1, vertical segments = 2.
β’ Pick 4 (from y) -> horizontal cut, Cost = 4 Γ vertical segments = 8,
Now, horizontal segments = 2, vertical segments = 2.
β’ Pick 3 (from x) -> vertical cut, Cost = 3 Γ horizontal segments = 6,
Now, horizontal segments = 2, vertical segments = 3.
β’ Pick 2 (from x) -> vertical cut, Cost = 2 Γ horizontal segments = 4,
Now, horizontal segments = 2, vertical segments = 4.
β’ Pick 2 (from y) -> horizontal cut, Cost = 2 Γ vertical segments = 8,
Now, horizontal segments = 3, vertical segments = 4.
β’ Pick 1 (from x) -> vertical cut, Cost = 1 Γ horizontal segments = 3,
Now, horizontal segments = 3, vertical segments = 5.
β’ Pick 1 (from x) -> vertical cut, Cost = 1 Γ horizontal segments = 3,
Now, horizontal segments = 3, vertical segments = 6.
β’ Pick 1 (from y) -> horizontal cut, Cost = 1 Γ vertical segments = 6,
Now, horizontal segments = 4, vertical segments = 6.
So, the total cost = 4 + 8 + 6 + 4 + 8 + 3 + 3 + 6 = 42.
Example 2
Input: n = 4, m = 4, x[] = [1, 1, 1], y[] = [1, 1, 1]
Output: 15
Explanation:
Initially no. of horizontal segments = 1 & no. of vertical segments = 1.
Optimal way to cut into square is:
β’ Pick 1 (from y) -> horizontal cut, Cost = 1 Γ vertical segments = 1,
Now, horizontal segments = 2, vertical segments = 1.
β’ Pick 1 (from y) -> horizontal cut, Cost = 1 Γ vertical segments = 1,
Now, horizontal segments = 3, vertical segments = 1.
β’ Pick 1 (from y) -> horizontal cut, Cost = 1 Γ vertical segments = 1,
Now, horizontal segments = 4, vertical segments = 1.
β’ Pick 1 (from x) -> vertical cut, Cost = 1 Γ horizontal segments = 4,
Now, horizontal segments = 4, vertical segments = 2.
β’ Pick 1 (from x) -> vertical cut, Cost = 1 Γ horizontal segments = 4,
Now, horizontal segments = 4, vertical segments = 3.
β’ Pick 1 (from x) -> vertical cut, Cost = 1 Γ horizontal segments = 4,
Now, horizontal segments = 4, vertical segments = 4
So, the total cost = 1 + 1 + 1 + 4 + 4 + 4 = 15.
π Constraints
$2 \le n, m \le 10^3$
$1 \le x[i], y[j] \le 10^3$
β
My Approach
The optimal approach uses a Greedy Strategy with Sorting and Two Pointers technique:
Greedy Algorithm with Two Pointers
Key Insight:
Always choose the cut with the highest cost first to minimize future multiplications.
When we make a vertical cut, it affects all current horizontal segments.
When we make a horizontal cut, it affects all current vertical segments.
Initialize Variables:
Sort both arrays
x[]
andy[]
in descending order.Use two pointers:
i
for x array andj
for y array.Track
h
(horizontal segments) andv
(vertical segments), both starting at 1.Maintain
cost
to accumulate the total cutting cost.
Greedy Selection:
Compare
x[i]
andy[j]
and always pick the larger cost.If
x[i] > y[j]
: Make vertical cut, cost +=x[i] * h
, incrementv
.Else: Make horizontal cut, cost +=
y[j] * v
, incrementh
.
Complete Remaining Cuts:
Process remaining cuts in the respective arrays.
Continue until all cuts are made.
Why This Works:
By choosing expensive cuts first, we minimize the number of segments they multiply with.
This ensures optimal cost distribution across all cuts.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n + m log m), where n and m are the sizes of arrays x[] and y[] respectively. The sorting dominates the time complexity as we need to sort both arrays in descending order, and the two-pointer traversal takes O(n + m) time.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables like pointers, counters, and the result. The sorting is done in-place.
π§βπ» Code (C++)
class Solution {
public:
int minCost(int n, int m, vector<int>& x, vector<int>& y) {
sort(x.rbegin(), x.rend());
sort(y.rbegin(), y.rend());
int h = 1, v = 1, i = 0, j = 0, cost = 0;
while (i < x.size() && j < y.size()) {
if (x[i] > y[j]) cost += x[i++] * h, v++;
else cost += y[j++] * v, h++;
}
while (i < x.size()) cost += x[i++] * h;
while (j < y.size()) cost += y[j++] * v;
return cost;
}
};
β Code (Java)
class Solution {
public static int minCost(int n, int m, int[] x, int[] y) {
Arrays.sort(x); Arrays.sort(y);
int h = 1, v = 1, i = x.length - 1, j = y.length - 1, cost = 0;
while (i >= 0 && j >= 0) {
if (x[i] > y[j]) { cost += x[i--] * h; v++; }
else { cost += y[j--] * v; h++; }
}
while (i >= 0) cost += x[i--] * h;
while (j >= 0) cost += y[j--] * v;
return cost;
}
}
π Code (Python)
class Solution:
def minCost(self, n, m, x, y):
x.sort(reverse=True); y.sort(reverse=True)
h = v = i = j = cost = 0; h = v = 1
while i < len(x) and j < len(y):
if x[i] > y[j]: cost += x[i] * h; v += 1; i += 1
else: cost += y[j] * v; h += 1; j += 1
while i < len(x): cost += x[i] * h; i += 1
while j < len(y): cost += y[j] * v; j += 1
return cost
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated