Topic
Enter an array of positive integers, combine all the numbers in the array into a number, and print the smallest of all the numbers that can be stitched together. For example, enter the array {3,32,321}, then print out the minimum number that these three numbers can be ranked as 321323.
Solving
Two numbers A, b to a number C
There are only two possible types of C: AB or BC
The title requires the smallest number of pieces, select the smallest number in the AB, BC as the answer
When there are three numbers: A, B, c
There are six ways of splicing: ABC, ACB, BAC, BCA, Cab, CBA
You can consider this:
First splicing two of the number to make the number of the smallest, the last spell and the rest of the number of stitching, making the number of the smallest
This is a recursive process.
When there is n number, choose two number so that the two number is the smallest, where the time complexity is O( N 2 )
Import Java.util.*;import java.util.ArrayList; Public classSolution { Public Static void Main(String []args) {int[] p =New int[]{3,5,1,4,2}; Solution s =NewSolution (); String res = S.printminnumber (p); System. out. println (RES); } PublicStringPrintminnumber(int[] numbers) {String result ="";if(Numbers = =NULL|| Numbers.length = =0)returnResult Arraylist<integer> A =NewArraylist<integer> (); for(intNum:numbers) {a.add (num); } while(A.size ()! =1) {tominnumbers (A); for(intA:A) {System. out. Print (A +"\ T"); } System. out. println (); }returnA.Get(0)+""; } Public void tominnumbers(arraylist<integer> A) {intSize = A.size ();intAId =-1;intBId =-1;intb;intmin = Integer.max_value; for(intI=0; i<size;i++) {a = a.Get(i); for(intj = i+1; j<size;j++) {b = A.Get(j);intAB =Join(A, B);if(ab< min) {aId = i; BId = j; min = ab; }intBA =Join(B,a);if(ba< min) {aId = j; BId = i; min = ba; } } }//Remove the large ID first if(aid<bid) {A.remove (bId); A.remove (AID); }Else{A.remove (aId); A.remove (BID); } a.add (min); } Public int Joinstr(intAintb) {String L = a +""; String r = B +""; String lr = l + R;returnInteger.valueof (LR); } Public int Join(intAintb) {intn =1;intTmpb= b; while(tmpb!=0) {n *=Ten; Tmpb/=Ten; }returnA*n+b; }}
However, the above program submission is wrong
Logarithmic group: 3,5,1,4,2
Output Result:
3 5 1 4 23 5 4 12 5 12 34 34 125 12534
Each time a merge is minimized based on the combined result, you can see that the merge operation is not the global minimum, because the other number may be in the middle of the combined two number.
The local minimum is not the global minimum and cannot be judged by the combined value of two numbers, the point of the problem is who should be in front of who should be in the post.
When a is in front of B, C may be in the middle of a, B, all can not immediately merge A, B, and should determine the position of a, B successively, C according to the size of the merger, placed in a, B appropriate position. This is a sort problem, and the collation is redefined: compare the size of a and B after merging
Fast sorting algorithm
Import java.util.ArrayList; Public classSolution { PublicStringPrintminnumber(int[] numbers) {String result ="";if(Numbers = =NULL|| Numbers.length = =0)returnResultintleft =0;intright = Numbers.length-1; QuickSort (Numbers,left,right); for(intnum:numbers) {result +=num; }returnResult }//Quick-line Public void QuickSort(int[] numbers,intLeftintright) {if(Left>right)return;intid = partition (numbers,left,right); QuickSort (numbers,left,id-1); QuickSort (numbers,id+1, right); }//partition ID Public int Partition(int[] numbers,intLeftintright) {intx = Numbers[right];inti = left-1; for(intj = Left;j< right;j++) {if(Compare (Numbers[j],x) <=0) {i++; Swap (NUMBERS,I,J); }} i++; Swap (numbers,i,right);returnI }//Exchange Public void Swap(int[] numbers,intIintj) {intTMP = Numbers[i]; Numbers[i] = Numbers[j]; NUMBERS[J] = tmp; }//Comparison rules Public int Compare(intAintb) {String L = a +""; String r = B +""; String lr = l + R; String RL = R + L;returnLr.compareto (RL); }}
To rank the array into the smallest number