Code RoomPerfect squares sum
HardPrep Room Coding #983

Perfect squares sum

CodingAlgorithms & data structuresSenior–Staff~30 min

Given a positive integer n (1 <= n <= 1e6), return the least number of perfect squares (1, 4, 9, 16, ...) that sum to n. For example 12 = 4 + 4 + 4 needs 3, and 13 = 4 + 9 needs 2. An O(sqrt(n)) number-theoretic solution exists; the O(n*sqrt(n)) DP also works but is slower.

Implement
num_squares(n: int) → int
Examples
in[12]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[12]3not run yetsample