Problem 507564 · medium · Phase 05 Advanced Algorithms & Graphs

Square Tile Packing

dynamic programming · 1-D dp · min count · unbounded choice

A workshop cuts square tiles of every integer side (1x1, 2x2, 3x3, ...). A customer wants tiles whose areas add up to exactly area. Return the smallest number of tiles that works. (An answer always exists because 1x1 tiles can fill anything.)

Examples

Input:  area = 13
Output: 2
Explanation: 9 + 4.

Input:  area = 12
Output: 3
Explanation: 4 + 4 + 4; no two squares sum to 12.

Constraints

  • 1 <= area <= 5000
  • Target complexity: O(area * sqrt(area)) time.

Goals

  • Write a min-count recurrence over all square summands
  • Bound the inner loop by the square root of the target
Starting Python…