LeetCode 279: Perfect Squares, Deriving Dynamic Programming from Remaining Sums

Problem Requirements Given a positive integer n, return the minimum number of positive perfect squares whose sum is exactly n. A perfect square is the square of an integer, such as 1 = 1², 4 = 2², 9 = 3², or 16 = 4². The sum must equal n exactly. You may use the same perfect square any number of times. Return only the count, not the decomposition. The order of the terms does not affect the answer. Constraint: 1 <= n <= 10^4. Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4 uses 3 perfect squares. Input: n = 13 Output: 2 Explanation: 13 = 9 + 4 uses 2 perfect squares. This guide builds the solution in Python 3.9+. It assumes familiarity with functions, loops, and basic recursion, and develops the dynamic programming reasoning step by step. ...

October 8, 2026 · 10 min · map[name:Jeanphilo]