Skip to the code
cf.
+25 XP

#521 Fewest Square Thumbnails

medium

A photo wall is built from square thumbnails. A thumbnail of side k covers k * k cells, and any side length can be used any number of times. You need thumbnails whose areas add up to exactly n cells.

Write fewestSquares(n) that returns the fewest thumbnails needed.

Example 1

Input: n = 18
Output: 2

9 + 9: two thumbnails of side 3.

Example 2

Input: n = 7
Output: 4

4 + 1 + 1 + 1. No two or three squares add up to 7.

Constraints

  • 1 <= n <= 10000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).