Perfect Squares
Last updated
Last updated
There is a purely mathematical solution for this as well but I won't cover it here
The number of perfect square numbers (PSN) that sum to a perfect square is 1
The minimum number of PSNs to form n
is found by trying all possible combinations of perfect square numbers
Using PSN p
means we have to find the minimum number of PSNs to form n - p
after
We add to because we are using as the first perfect square.
Recurrence pattern: past lives states
Some states rely on multiple previously computed state. This contrasts the state caching optimization as is not fixed.
Trick: using impossibly high/low numbers to initialize
If min(dp[i], ...)
is used, then use an impossibly high number to initialize dp[i]
Otherwise, if max(dp[i], ...)
is used, then use an impossibly low number or 0
if no other states can be 0
.