15% off all plans & roadmaps Use code
See plansProblem editorial
Given a positive integer n, consider the set of perfect squares that do not exceed n. A perfect square is the product of an integer with itself, such as 1, 4, 9, or 16.
Perfect Squares is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Coin Change / Unbounded Knapsack Style) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
The reference solution runs in O(n * sqrt(n)) time and O(n) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Perfect Squares is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Coin Change / Unbounded Knapsack Style) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.