#279

Perfect Squares

specialist · 770 · lc medium +31 · verified · 56.3% accepted · 11,805 likes · top 50%

play →

Description

Given a positive integer n, return the minimum number of perfect square numbers that add up to n. A perfect square is any integer that equals some integer multiplied by itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.

Example 1:

Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.

Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.

Code

1
2
3