Topic of thought of enumeration algorithm

Source: Internet
Author: User
Tags square root

An example of the idea of enumeration algorithm

Solution0:

Solution1:

idea 1: Because the maximum value of the direct inverse enumeration can be: The largest is 9876543210, the smallest is the title of the 1026753849. And then we're going to judge if it's exactly 10 digits 0~9. And then judging is not exactly the square number

1 Static voidSolution1 () {2 Long i;3      for(i = 9876543210L; i > 1026753849; i--) {4         DoubleQ = (int) Math.sqrt (i*1.0);5         if(q*q==i) {6             if(Test (i)) {7 System.out.println (i);8                  Break;9             }Ten         } One     } A}
View Code

Idea 2: The square root of the enumeration, the square root of 10 digits, the minimum is 30000, the maximum we set here is 100000, more than enough. Then we calculate x according to Y, so that x must be a total squared, and we don't have to check it. Just check if x is exactly 10 digits:

1 Static voidSolution2 () {2      for(inti=100000;i>30000;i--){3         LongX= (Long) i*i;4        //System.out.println (x);5         if(Test (x)) {6 System.out.println (x);7              Break;8         }9     }Ten}
View Code

determine if the common code is duplicated:

1 Private Static BooleanTest (Long i) {2Set<integer> myset=NewHashset<>();3     Longx=i;4      while(x!=0){5         intL = (int) (x% 10);6 Myset.add (l);7x/=10;8     }9     returnMyset.size () ==10;Ten}
View Code

Summary: The complexity of this algorithm is much lower than the previous algorithm. Enumeration is probably the 10^5 magnitude, judging whether it is exactly 10 numbers and O (10) complexity, a total of about 10^6 magnitude.

Solution2:

Idea 1: It is not difficult to see the topic, directly enumerate each bit of the array, plus the corresponding difference, and then iterate through the array to see if there is a corresponding value in this array, and then processing the repetition can get the answer, but this complexity is O (N2), where an O (N) is an ingenious solution

1  //O (N) example12     Static voidSolution2 () {3         intN,k,ans=0;4n=in.nextint ();5k=in.nextint ();6Set<integer> MySet =NewHashset<>();7          for(inti=0;i<n;i++){8 Myset.add (In.nextint ());9         }Ten          for(Integer x:myset) { One             if(Myset.contains (x+k)) { Aans++; -             } -         } the System.out.println (ans); -}

Solution3:

Let's look at another question, called a brick wall. This problem is adapted from online Facebook last year's interview question, is Hihocoder's 1494 questions (https://hihocoder.com/problemset/problem/1494)

Idea: I get this topic, intuition is not able to simulate, the bricks seem to have no connection, but think carefully to see the figure below, the red line is the best wall-piercing answer, so we can think, in the gap between the two bricks is the answer we are looking for, and can find

In the case where the length of the brick is inconsistent, where the width is coincident, this place we can be represented by the x-coordinate, then the number of repetitions of the x-coordinate at the time of reading is the position to be penetrated.

1 Static voidSolution3 () {2         intN;3Map<integer,integer> mycoor=NewHashmap<>();4n=in.nextint ();5          for(inti=0;i<n;i++){6             intN,cnt=0;7n=in.nextint ();8              for(intj=0;j<n;j++){9                 intxk=in.nextint ();Tencnt+=xk; OneInteger x1 =Mycoor.get (CNT); A                 if(x1==NULL) x1=0; -                 if(j!=n-1) Mycoor.put (cnt,++x1); - System.out.println (x1); the             } -         } -         intMax=0; -          for(map.entry<integer,integer>X:mycoor.entryset ()) { +max=Math.max (X.getvalue (), max); -         } +SYSTEM.OUT.PRINTLN (nmax); A}

Solution4:

The above topic seems to be a range of enumeration topics, but in fact need full AC need to pay attention to a lot of places, if shorten the time complexity of constructing analytic equation;

The general practice is to enumerate the equation A^2+b^2+c^2+d^2=n range, but the time complexity is too high: "Here the/4/3/2/1= is reopened to construct an incremental law that satisfies the proposed requirements"

So it can be divided into partial tectonic solution: the latter part of the solution is constructed in advance:

1 Static voidSolution1 () {2Map<integer,integer> f=NewHashmap<>();3         intn=in.nextint ();4         //structural decomposition of the rear5          for(intC=0;c*c<=n/2;c++){6              for(intd=c;d*d+c*c<=n;d++){7                 if(!f.containskey (d*d+c*c)) {8F.put (c*c+d*d,c);9                 }Ten             } One         } A         //The first part of the construction solution -          for(inta=0;a*a*4<=n;a++){ -              for(intb=a;b*b+a*a<=n/2;b++){ the                 if(F.containskey (n-a*a-b*b)) { -Integer C = F.get (N-a * A-B *b); -                     intD= (int) math.sqrt ((n-a*a-b*b-c*c) +1e-3); -System.out.println (A + "" +b+ "" +c+ "" +d); +                     return; -                 } +             } A         } at}

Topic of thought of enumeration algorithm

Related Article

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.