Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...
) which sum to n.
Example 1:
Example 2:
分析
背包问题,把v[i]装满,最少需要多少step。
状态方程:v[i] = min(v[i], v[i-j*j]+1)
最后答案v[n]
java,i,j都从1开始,跳过0的情况。
Last updated
Was this helpful?