Math Fundamentals18 sections · 814 units
Open in Course

Problem - Perfect Squares

LeetCode 279

Given an integer nn, return the least number of perfect square numbers that sum to nn. For example, 12=4+4+412 = 4 + 4 + 4, so the answer is 33.

This problem uses sequences in a different way: the sequence of perfect squares 1,4,9,16,25,...1, 4, 9, 16, 25, ... forms the building blocks.

You need dynamic programming here, but understanding that you're working with the sequence k2k^2 for k=1,2,3,...k = 1, 2, 3, ... helps you structure the solution.