VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-cost-cut-board-squares/

⇱ Minimum Cost to cut a board into squares - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Cost to cut a board into squares

Last Updated : 13 Sep, 2025

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: 

Input: x[] = [2, 1, 3, 1, 4], y[] = [4, 1, 2], n = 4, m = 6
Output: 42
Explanation:
šŸ‘ Image

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.

Input: x[] = [1, 1, 1], y[] = [1, 1, 1], n = 4, m = 4
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.

[Naive Approach] Try All Permutations - O((n+m)!Ɨ(n+m)) Time and O(n+m) Space

The idea is to generate all possible permutations of the given cuts and then calculate the cost for each permutation. Finally, return the minimum cost among them.

Note: This approach is not feasible for larger inputs because the number of permutations grows factorially as (m+n-2)!.
For each permutation, we must calculate the cost in O(m+n) time. Hence, the overall time complexity becomes O((m+nāˆ’2)!Ɨ(m+n)).

[Expected Approach] Using Greedy Technique - O( n (log n)+m (log m)) Time and O(1) Space

The idea is to make the most expensive cuts first using a greedy approach. The observation is that choosing the highest cost cut at each step reduces future costs by affecting multiple pieces at once. We sort the vertical (x) and horizontal (y) cut costs in descending order, then iteratively pick the larger one to maximize cost savings. The remaining cuts are processed separately to ensure all sections are split optimally.

What happens when we make a cut?

  • Horizontal cut → you are cutting across the width, so the number of horizontal strips increases (hCount++). But the cost is multiplied by vCount (the number of vertical strips), because the horizontal cut has to pass through all vertical segments.
  • Vertical cut → you are cutting across the height, so the number of vertical strips increases (vCount++). But the cost is multiplied by hCount (the number of horizontal strips), because the vertical cut has to pass through all horizontal segments.

Steps to solve the problem:

  • Sort both x and y arrays in descending order.
  • Use two pointers, one for x and one for y, starting from the largest value and moving toward smaller values.
  • Maintain hCount and vCount to track how many segments each cut affects and update them accordingly.
  • Iterate while both x and y have unprocessed cuts, always choosing the larger cost to minimize overall expenses.
  • If x has remaining cuts, process them with hCount multiplier; similarly, process remaining y cuts with vCount.
  • Accumulate total cost at each step using the formula: cut cost * number of affected pieces, ensuring minimal cost.

Output
42
Comment
Article Tags:
Article Tags: