For the number of i
, the minimum sum(dp[i]
) is the min of (dp[i - 1]
, dp[i - 4]
, dp[i - 9]
…) + 1
dp[i]
represents the minimum perfect square numbers that sum to i
.
Every natural number can be represented as a sum of four non-negative integer squares. That is, the squares form an additive basis of order four
and what’s more, only when $n \neq 4^k \times (8m + 7)$, n can be represented up to three number squares sum. so in the opposite, when $n = 4^k \times (8m + 7)$, the number can be represents
So we the the answer is 1, this number it self is a perfect square number,
when the answer is 4, this number can be represented as $4^k \times (8m + 7)$,
when the answer is 2, we need to loop for each a, b to find that $n = a^2 + b^2$,
If all three conditions above are not satisfied, then the answer would be 3.
Dynamic Programing:
1 | class Solution { |
Math:
1 | class Solution { |