Find the Water king

Source: Internet
Author: User

I. Topics and Requirements
    • Topic, three-line design of a irrigation forum. Information college students like to exchange irrigation above, legend in the forum there is a "water king", he not only likes to post, but also reply to other ID issued by each post. The "Water King" has been rumored to have posted more than half the number of posts.
    • Request, if you have a list of posts (including replies) for the current forum, where the author ID of the post is also in it, can you quickly find the legendary water king?

Second, design ideas

    • First see this problem, the most basic solution is to count the number of each ID post, the final comparison size, so you can find "Water king", however, this method is undoubtedly the most troublesome, time complexity of O (n^2);
    • Then, to reduce the complexity, it is necessary to consider the conditions of the topic, "Water king" post more than half of the number of posts, then if the ID of the post is ordered, then the center of the ID must be water king, so that the second solution was born, but the time complexity of O (n*log^n);
    • To make the time complexity O (n), we can count all the IDs, starting with the first one, counting to single=0, marking as ID0, and traversing backwards if ID1 is the same as ID0, then singl+1 (which is equivalent to counting the same ID once), otherwise single- 1 (equivalent to no longer consider the ID, delete directly), R if single=0, mark as ID (x); The last result is the ID (x);

Third, the source code

  • //waterking.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include"iostream"using namespacestd;voidData (intLengthinta[]) {cout<<"Please enter a list of IDs:"<<Endl;  for(intI=0; i<length;i++) {cin>>A[i]; }}intMain () {intlength; intsingle=0; intID;//set the semaphorecout<<"Please enter the number of posts:"; CIN>>length; int* waterking=New int[length];    Data (length,waterking);  for(intI=0; i<length;i++)    {        if(single==0) {ID=Waterking[i]; Single++; }        Else if(waterking[i]==ID) { single++; }        Else{ Single--; }} cout<<"Water King Absalom:"<<ID<<Endl; return 0;}

    Iv. Results of the experiment

    V. Analysis of results

    This is a very typical requirement algorithm optimization, code is not long, mainly to consider what method should be implemented, how to achieve the optimization of the code. To achieve the optimization of the code, the first thing to solve is to find the core of the problem, from the core, in order to have ideas to explore ways to solve the problem. In this problem, according to the conditions we can know, more than half of the posts are an ID, so, as long as the median value, you can find the so-called Water King, but, this optimization is not perfect, to reduce his time complexity to O (n^2), you need to consider the same ID and the relationship between different IDs, for different IDs , direct exclusion, then you can rule out the existence of water King, because the water king Post number is large enough, then the last remaining must be the water king.

    In this problem, the code optimization process is not an overnight, but a gradual thinking in depth, to find the key point of the problem to gradually achieve the ideal optimization process. And this process is exactly what we need to focus on.

Find the Water king

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.