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