Lintcode: Perfect Square

Source: Internet
Author: User
Tags lintcode

Topic

Give a positive integer n to find several complete squares (e.g. 1, 4, 9, ...) Make their sum equal to N. You need to have the fewest number of squares.

Sample Example

Give n = 12 , return 3 because 12 = 4 + 4 + 4 .
Give n = 13 , return 2 because 13 = 4 + 9 .

Solving

Title tag: Depth-First traversal

The following deep search, by finding all the results, and then find the shortest one

 Public classSolution {/**     * @paramn a positive integer *@returnAn integer*/     Public intNumsquares (intN) {//Write Your code here        intUp = (int) math.sqrt (n); ArrayList<ArrayList<Integer>> result =NewArraylist<arraylist<integer>>(); ArrayList<Integer> list =NewArraylist<integer>(); intsum = 0;        DFS (RESULT,LIST,UP,SUM,N); intmin = result.get (0). Size ();  for(intI=0;i<result.size (); i++) {min=math.min (Min,result.get (i). Size ()); }        returnmin; }     Public voidDFS (arraylist<arraylist<integer>> result,arraylist<integer> list,intStartintSumintN) {        if(n = =sum) {Result.add (NewArraylist<integer>(list)); return; }        if(Start==0 | | sum>N)return;  for(inti=start;i>=1;i--){                        if(Sum+i*i >N) {                Continue;                        } list.add (i); DFS (Result,list,i,sum+i*i,n); List.remove (List.size ()-1); }    }}

Input:

1684
时候内存溢出

不记录结果,只统计次数,改成下面的在1684时候时间超时
 Public classSolution {/**     * @paramn a positive integer *@returnAn integer*/     intMincount =Integer.max_value;  Public intNumsquares (intN) {//Write Your code here        intUp = (int) math.sqrt (n); ArrayList<ArrayList<Integer>> result =NewArraylist<arraylist<integer>>(); ArrayList<Integer> list =NewArraylist<integer>(); intsum = 0;        DFS (RESULT,LIST,UP,SUM,N); //int min = result.get (0). Size (); //for (int i=0;i<result.size (); i++) {//min = math.min (Min,result.get (i). Size ()); // }        returnMincount; }     Public voidDFS (arraylist<arraylist<integer>> result,arraylist<integer> list,intStartintSumintN) {        if(n = =sum) {            //Result.add (new arraylist<integer> (list));Mincount =math.min (Mincount,list.size ()); return; }        if(Start==0 | | sum>N)return;  for(inti=start;i>=1;i--){                        if(Sum+i*i >N) {                Continue;                        } list.add (i); DFS (Result,list,i,sum+i*i,n); List.remove (List.size ()-1); }    }}

Dynamic Programming Problem Solving

Reference links

If a number x can be represented as an arbitrary number a plus a squared number of BXB, that is, X=A+BXB, then the number of squares that can be composed of a minimum squared number plus 1 (because b*b is already a square number).

 Public classSolution {/**     * @paramn a positive integer *@returnAn integer*/     intMincount =Integer.max_value;  Public intNumsquares (intN) {//Write Your code here                    int[] DP =New int[N+1]; //The result of all non-square numbers is set to maximum, which is guaranteed to not be selected after comparisonArrays.fill (DP, integer.max_value); //Place the result of all squares 1         for(inti = 0; I * I <= N; i++) {Dp[i* I] = 1; }        //from small to large to find any number a         for(intA = 0; a <= N; a++){            //from small to large to find square number BXB             for(intb = 0; A + b * b <= n; b++){                //because A+b*b is probably the square number itself, we're going to take two of the smallerDp[a + b * b] = Math.min (Dp[a] + 1, dp[a + b *b]); }        }        returnDp[n]; }}    



Lintcode: Perfect Square

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.