Medium · Dynamic Programming

Perfect Squares

Given an integer n, return the least number of perfect squares (1, 4, 9, 16 and so on, each usable any number of times) that sum to n.

Examples

Example 1

n = 12

Output: 3 squares

Example 2

n = 7

Output: 4 squares

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Dynamic Programming problems