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
π 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:
ifor x array andjfor y array.Track
h(horizontal segments) andv(vertical segments), both starting at 1.Maintain
costto 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++)
β Code (Java)
π Code (Python)
π§ 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