Skip to content
Onsite

Perfect Squares

MediumP3Coding

Return the least number of perfect squares (1, 4, 9, 16, ...) that add up to n. The same square may be used more than once.

Example 1

Input
n = 12
Output
3
Why
12 = 4 + 4 + 4.

Example 2

Input
n = 13
Output
2
Why
13 = 4 + 9.

Example 3

Input
n = 1
Output
1

Constraints

  • 1 <= n <= 10^4

Hints

Run checks the 3 visible tests, Submit all 12.