[Leetcode]Perfect Squares(DP and Math Solution)

**Perfect Squares My Submissions Question
Total Accepted: 16355 Total Submissions: 55778 Difficulty: Medium
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.

For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

题意很简单,就是找出一个正整数,找出最少用几个完全平方数(不包括0)可以表示它,用动态规划做就可以了,然而用时788ms。

其实最小可以缩到4ms,根据[Lagrange’s four-square theorem]
任一个自然数都可以用4个完全平方数表示出来,所以这里分情况讨论就好了,注意可能包括0!
下面贴出788ms的代码,以及4ms的代码。

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

Legendre’s three-square theorem

class Solution 
{  
private:  
    int is_square(int n)
    {  
        int sqrt_n = (int)(sqrt(n));  
        return (sqrt_n*sqrt_n == n);  
    }

public:
    // Based on Lagrange's Four Square theorem, there 
    // are only 4 possible results: 1, 2, 3, 4.

    //根据Lagrange's Four Square theorem,有且只
    //有4种可能的结果:1,2,3,4
    int numSquares(int n) 
    {  
        // If n is a perfect square, return 1.

        //如果n是个完全平方数,返回1
        if(is_square(n)) 
        {
            return 1;  
        }

        // The result is 4 if n can be written in the 
        // form of 4^k*(8*m + 7). Please refer to 

        //如果n可以被写成4^k*(8*m + 7)的形式,
        //那么结果就是4,请参考Legendre's three-square theorem

        //这里多说一句,Legendre's three-square theorem是说
        //如果n不可以被写成4^k*(8*m + 7)的形式,那么n就可以
        //用三个整数的平方和表示。

        //这里判断的是:如果n可以被写成4^k*(8*m + 7)的形式
        //那么就直接返回4。这里的差别主要是要考虑Legendre's 
        //three-square theorem 里说的是整数,可能包括0,比如说
        //对于5 = 2^2 + 1^2 + 0^2.而我们要求的则不包括0.
        //感觉这里需要认真体会下这个细节:如果n可以被写成
        //4^k*(8*m + 7)的形式,那么就直接返回4

        while ((n & 3) == 0) // n%4 == 0  
        {
            n >>= 2;  //如果n能被4整除,那么n和n/4,
                      //其返回的值是相同的!
        }
        if ((n & 7) == 7) // n%8 == 7
        {
            return 4;
        }

        // Check whether 2 is the result.
        //检查2是不是结果
        int sqrt_n = (int)(sqrt(n)); 
        for(int i = 1; i <= sqrt_n; i++)
        {  
            if (is_square(n - i*i)) 
            {
                return 2;  
            }
        }  

        return 3;  //其他情况返回3
    }  
};

除此之外,这道题还有另外两种解法,Static Dynamic Programming和BFS。这里就不贴出来了,有兴趣的可以戳这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值