Tiling a Rectangle with the Fewest Squares Problem
Description
LeetCode Problem 1240.
Given a rectangle of size n x m, return the minimum number of integer-sided squares that tile the rectangle.
Example 1:

Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:

Input: n = 5, m = 8
Output: 5
Example 3:

Input: n = 11, m = 13
Output: 6
Constraints:
1 <= n, m <= 13
Solution
Approach: Backtracking on the First Empty Cell
Simple rules such as "cut off the largest square, then solve the rest", or splitting the rectangle into two smaller rectangles, do not always give the minimum: the best tiling of 11 x 13 uses 6 squares and cannot be cut into two rectangles by a straight line. With n, m <= 13 we can search all tilings instead, as long as we prune well.
Any tiling has a square whose top-left corner is the first uncovered cell in row-major order (top to bottom, left to right). So the search is:
- Find the first empty cell
(i, j). If every cell is covered, record the number of squares used. - The next square has its top-left corner at
(i, j). Its size can grow while the cells below(i, j)in columnjand to its right in rowiare free, and while it stays inside the rectangle. Everything above rowiis covered, so if that top row and left column are free, the whole square is free. - Try every size from the largest down to
1, recurse, then undo.
Two things keep it fast:
- Pruning: if the squares used so far plus one cannot beat the best tiling found so far, stop. The best starts at
n * m(all unit squares). - Largest square first: big squares cover the rectangle quickly, so a good tiling is found early and the pruning then cuts most other branches.
Each row's covered cells fit in one integer (m <= 13 bits), so placing a square is setting a block of bits in each of its rows. Shrinking the square from w to w - 1 clears only its bottom row and right column.
Complexity: exponential in the worst case, but with the pruning it finishes quickly for every size up to 13 x 13. The space is O(n) for the bitmasks plus O(n · m) recursion depth.
class Solution:
def tilingRectangle(self, n: int, m: int) -> int:
filled = [0] * n # bit j of filled[i] is set when cell (i, j) is covered
best = n * m # n * m unit squares always work
def dfs(i: int, j: int, used: int) -> None:
nonlocal best
if j == m: # end of the row
i, j = i + 1, 0
if i == n: # every cell is covered
best = used
return
if filled[i] >> j & 1:
dfs(i, j + 1, used)
return
if used + 1 >= best: # cannot beat the best tiling found so far
return
# (i, j) is the first empty cell: it is the top-left corner of the next square.
# The square can grow while the cells below and to the right are free.
size = 0
while i + size < n and j + size < m and not filled[i + size] >> j & 1 and not filled[i] >> (j + size) & 1:
size += 1
for r in range(i, i + size):
filled[r] |= ((1 << size) - 1) << j
# Try the largest square first, then shrink it by one row and column at a time.
for w in range(size, 0, -1):
dfs(i, j + w, used + 1)
filled[i + w - 1] ^= ((1 << w) - 1) << j # free the bottom row of the w x w square
for r in range(i, i + w - 1):
filled[r] ^= 1 << (j + w - 1) # and its right column
dfs(0, 0, 0)
return best