[LintCode]perfect-squares(DP)

题目链接:http://www.lintcode.com/zh-cn/problem/perfect-squares/

就是求最小价值的完全背包,初始化dp[i]=i,假设全是1的时候是最多的,之后就是完全背包了。不知道为什么python过不了,Java和C++都过了。

 1 public class Solution {
 2     public int numSquares(int n) {
 3         int[] dp = new int[n+1];
 4         for(int i=1; i<=n; i++) dp[i] = i;
 5         for(int i=2; i<=n; i++) {
 6             for(int j = 1; j*j <= i; j++) {
 7                 dp[i] = Math.min(dp[i], dp[i-j*j]+1);
 8             }
 9         }
10         return dp[n];
11     }
12 }
原文地址:https://www.cnblogs.com/kirai/p/5625866.html