Perfect Squares

Medium

Given an integer n, return the fewest number of perfect squares that sum to n.

Example 1

numSquares()
Input
n = 12
Output
3

Why

12 = 4 + 4 + 4

Example 2

numSquares()
Input
n = 13
Output
2

Why

13 = 4 + 9
Constraints
1 <= n <= 10^4

Topics

MathDynamic ProgrammingBreadth-First Search

Implement numSquares in the editor and return the answer — printing it is never graded.