[Leetcode]Perfect Squares(DP and Math Solution)

原创 2015年11月21日 10:34:16

**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。这里就不贴出来了,有兴趣的可以戳这里

版权声明:转载请注明出处

279. Perfect Squares-Leetcode(关于DP的再深入研究)

先上题目: Given a positive integer n, find the least number of perfect square numbers (for example, 1,...

DP (7) -- Ones and Zeroes, Longest Increasing Subsequence, Perfect Squares

Ones and Zeroes,Longest Increasing Subsequence,Guess Number Higher or Lower II
  • dwt0317
  • dwt0317
  • 2017年01月11日 14:55
  • 114

CodeForces 599D Spongebob and Squares(DP+math)

Spongebob and Squares Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u...

LeetCode——Perfect Squares

题目描述: 将数字n,分解成k个平方数之和。如12 = 4 + 4 + 4,13 = 9 + 4。求k的最小值。         题目解法: 初读题目,发现如果n本身就是平方数最完美不过了,这样...

leetcode_279. Perfect Squares

leetcode_279. Perfect Squares

leetcode谷歌面试题279. Perfect Squares

无权图广搜谷歌面试题

【LeetCode从零单刷】Perfect Squares

题目: Given a positive integer n, find the least number of perfect square numbers (for example, 1,...

[LeetCode] 279. Perfect Squares 解题报告

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, ...
  • a921122
  • a921122
  • 2017年02月19日 03:04
  • 151

leetcode--Perfect Squares

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1...

【LeetCode】Perfect Squares 解题报告

Perfect Squares[LeetCode]https://leetcode.com/problems/perfect-squares/Total Accepted: 15606 Total S...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Leetcode]Perfect Squares(DP and Math Solution)
举报原因:
原因补充:

(最多只允许输入30个字)