队列&栈//完全平方数

给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12
输出: 3 
解释: 12 = 4 + 4 + 4.

示例 2:

输入: n = 13
输出: 2
解释: 13 = 4 + 9.
class Solution {
public:
    int numSquares(int n) {
        while(n%4==0) n/=4;
        if(n%8==7) return 4;
        for(int i=0;i*i<=n;i++){
            int j=sqrt(n-i*i);
            if(i*i+j*j==n)
                {
                    return !!i+!!j;
                }
        }
        return 3;
    }
};
class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n+1,INT_MAX);
        dp[0] = 0;
        for(int i = 0; i <= n; i++){
            for(int j = 1; j*j+i <= n; j++){
                dp[i+j*j] = min(dp[i+j*j],dp[i]+1);
            }
        }
        return dp[n];//return dp.back()也可以
    }
};
class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(1,0);
        while(dp.size() <= n){
            int m = dp.size(), val = INT_MAX;
            for(int i = 1; i * i <= m; i++){
                val = min(val, dp[m-i*i]+1);
            }
            dp.push_back(val);
        }
        return dp.back();
    }
};
class Solution {
public:
    int numSquares(int n) {
        int dp[n];
        memset(dp,0,n*sizeof(n));
        int res=dfs(n,0,dp);
        return res;
    }
private:
    int dfs(int n,int count,int *dp){
        if(n==0) return count;
        int c=0;
        if((c=dp[n-1])!=0)
            return count+c;
        int res=INT_MAX;
        int j=(int)sqrt(n);
        for(int i=j;i>=(j/2+1);i--){
            int num=n-pow(i,2);
            int c=dfs(num,count+1,dp);
            res=min(res,c);
        }
        dp[n-1]=res-count;
        return res;
    }
};

 

posted @ 2018-11-11 13:19  strawqqhat  阅读(81)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }