Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Perfect Squares

Medium
dynamic-programmingmathbfs

Return the least number of perfect squares (1, 4, 9, 16, …) that sum to n.

Input: one line, the integer n. Output: the minimum count.

Example 1
Input:
12
Output:
3

4 + 4 + 4.

Example 2
Input:
13
Output:
2

4 + 9.

Constraints

  • 1 ≤ n ≤ 10^4
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.