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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Dynamic Programming problems