Fewest Integer Squares Tiling a Rectangle

Quick Overview

Fewest Integer Squares Tiling a Rectangle. Tile an n-by-m rectangle completely with integer-sided squares, without overlaps or uncovered regions.

Fewest Integer Squares Tiling a Rectangle

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Tile an n-by-m rectangle completely with integer-sided squares, without overlaps or uncovered regions. Return the smallest number of squares. A solution may use different square sizes and need not arise from repeatedly cutting the rectangle into two rectangles. ```cpp class Solution { public: int tilingRectangle(int n, int m); }; ``` ### Examples **Example 1:** ```text Input: n = 2, m = 4 Output: 2 Explanation: Two2-by-2 squares tile it. One square cannot equal a nonsquare rectangle. ``` **Example 2:** ```text Input: n = 1, m = 5 Output: 5 Explanation: The height permits only1-by-1 squares, so five are necessary. ``` ### Constraints - Both rectangle dimensions are integers in `[1,13]`. ### Contract considerations Only the minimum count is returned. Every tiling must cover the rectangle without overlaps; no particular construction is prescribed. ```hint Read the boundary condition May tiles have different side lengths, and must they cover every part of the rectangle exactly once? ``` ### Boundary reminders Only the minimum count is returned. Every tiling must cover the rectangle without overlaps; no particular construction is prescribed.

Overview: Fewest Integer Squares Tiling a Rectangle. Tile an n-by-m rectangle completely with integer-sided squares, without overlaps or uncovered regions.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Tile an n-by-m rectangle completely with integer-sided squares, without overlaps or uncovered regions. Return the smallest number of squares. A solution may use different square sizes and need not arise from repeatedly cutting the rectangle into two rectangles.

class Solution {
public:
    int tilingRectangle(int n, int m);
};

Examples

Example 1:

Input: n = 2, m = 4
Output: 2
Explanation: Two2-by-2 squares tile it. One square cannot equal a nonsquare rectangle.

Example 2:

Input: n = 1, m = 5
Output: 5
Explanation: The height permits only1-by-1 squares, so five are necessary.

Constraints

  • Both rectangle dimensions are integers in [1,13] .

Contract considerations

Only the minimum count is returned. Every tiling must cover the rectangle without overlaps; no particular construction is prescribed.

Boundary reminders

Only the minimum count is returned. Every tiling must cover the rectangle without overlaps; no particular construction is prescribed.