279. Perfect Squares

https://blog.csdn.net/xinqrs01/article/details/55095134

https://www.cnblogs.com/grandyang/p/4800552.html

当前的数一定可以通过之前的数加上数的平方获得

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n+1,INT_MAX);
        dp[0] = 0;
        for(int i = 1;i <= n;i++){
            for(int j = 1;j*j <= i;j++){
                dp[i] = min(dp[i - j*j] + 1,dp[i]);
            }
        }
        return dp[n];
    }
};

 

posted @ 2019-03-07 17:20  有梦就要去实现他  阅读(90)  评论(0编辑  收藏  举报