Minimum Cost to cut a board into squares in Python

Suppose we have a board of length p and width q; we have to break this board into p*q number of squares such that cost of breaking is as minimum as possible. Cutting cost for each edge will be given.

The key insight is to use a greedy approach: always make the most expensive cut first, because expensive cuts will affect more pieces if made later.

Problem Understanding

Given:

  • X_slice: costs of horizontal cuts
  • Y_slice: costs of vertical cuts

We need to find the minimum total cost to cut the board into unit squares.

Board to be cut into squares Vertical cuts Horizontal cuts

Algorithm Steps

To solve this, we follow these steps:

  • Sort both cut arrays in descending order
  • Use greedy approach: always choose the more expensive cut
  • Track number of horizontal and vertical pieces
  • Each cut multiplies by the number of perpendicular pieces

Example

Let us see the implementation with X_slice = [3,2,4,2,5], Y_slice = [5,2,3]:

def minCost(X_slice, Y_slice, m, n):
    res = 0
    X_slice.sort(reverse=True)
    Y_slice.sort(reverse=True)
    horizontal = 1
    vertical = 1
    i = 0
    j = 0
    
    while i < m and j < n:
        if X_slice[i] > Y_slice[j]:
            res += X_slice[i] * vertical
            horizontal += 1
            i += 1
        else:
            res += Y_slice[j] * horizontal
            vertical += 1
            j += 1
    
    total = 0
    while i < m:
        total += X_slice[i]
        i += 1
    res += total * vertical
    
    total = 0
    while j < n:
        total += Y_slice[j]
        j += 1
    res += total * horizontal
    
    return res

# Test the function
m = 6
n = 4
X_slice = [3, 2, 4, 2, 5]
Y_slice = [5, 2, 3]
result = minCost(X_slice, Y_slice, m-1, n-1)
print(f"Minimum cost: {result}")
Minimum cost: 65

How It Works

The algorithm works by:

  • Sorting cuts in descending order to prioritize expensive cuts
  • Greedy selection: choosing the more expensive cut at each step
  • Multiplying cost by the number of existing perpendicular pieces
  • Processing remaining cuts after one array is exhausted

Step-by-Step Trace

def minCostWithTrace(X_slice, Y_slice, m, n):
    print(f"Original X_slice: {X_slice}")
    print(f"Original Y_slice: {Y_slice}")
    
    X_slice.sort(reverse=True)
    Y_slice.sort(reverse=True)
    
    print(f"Sorted X_slice: {X_slice}")
    print(f"Sorted Y_slice: {Y_slice}")
    
    res = 0
    horizontal = 1
    vertical = 1
    i = j = 0
    
    print("\nStep-by-step execution:")
    while i < m and j < n:
        if X_slice[i] > Y_slice[j]:
            cost = X_slice[i] * vertical
            res += cost
            print(f"Horizontal cut {X_slice[i]} × {vertical} pieces = {cost}")
            horizontal += 1
            i += 1
        else:
            cost = Y_slice[j] * horizontal
            res += cost
            print(f"Vertical cut {Y_slice[j]} × {horizontal} pieces = {cost}")
            vertical += 1
            j += 1
    
    return res

# Test with trace
X_slice = [3, 2, 4, 2, 5]
Y_slice = [5, 2, 3]
result = minCostWithTrace(X_slice.copy(), Y_slice.copy(), 5, 3)
print(f"\nFinal minimum cost: {result}")
Original X_slice: [3, 2, 4, 2, 5]
Original Y_slice: [5, 2, 3]
Sorted X_slice: [5, 4, 3, 2, 2]
Sorted Y_slice: [5, 3, 2]

Step-by-step execution:
Horizontal cut 5 × 1 pieces = 5
Vertical cut 5 × 2 pieces = 10
Horizontal cut 4 × 2 pieces = 8
Vertical cut 3 × 3 pieces = 9
Horizontal cut 3 × 3 pieces = 9

Final minimum cost: 41

Conclusion

The minimum cost board cutting problem uses a greedy approach where we always make the most expensive cut first. This ensures that expensive cuts affect fewer pieces, minimizing the total cost. The algorithm runs in O(n log n) time due to sorting.

Updated on: 2026-03-25T09:58:58+05:30

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements