FunctionRacerProblemsRace

Fewest Perfect Squares

MediumDynamic programmingPython, JavaScript, TypeScript, Java, C++

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

Example 1

n 12
returns 3

4 + 4 + 4.

Example 2

n 13
returns 2

4 + 9.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Dynamic programming problems