Looking for a post "water king"

Source: Internet
Author: User

topic Background: Tango is a pilot project of Microsoft Research Asia. The staff and interns at the Institute are very fond of exchanging water on tango. Legend has it that Tango has a big "water king", and he not only likes to post, but also replies to each of the posts sent by other IDs. Rumor has it that the "water king" posted more than half the total number of posts. If you have a list of all posts (including replies) on the current forum, and the ID of the post author is also in the table, can you quickly find out the legendary tango water king?
programme I: Sort the ID of the post. Find more than half of the ID numbers for the queued ID and the time complexity for sorting is O (N*logn). Determine if half of the time complexity is O (N) in the sorted ID, because the ID of the "Water King" post must be the N/2 number. The total time complexity isO (N*LOGN).
Scenario Two:Use the partion () function in the quick sort to find it. You don't need to sort the IDs at this time, just find the number of INDEX=N/2. Before index is less than Data[index], after index is greater than data[index]. At this point the time complexity is O (N). Finally, the number is not "Water king", its time complexity is O (N).
Programme III:According to the characteristics and laws of the data to find the general idea is: because we do not know that is the "Water king", so assume that each ID may be the water king, here we first assume that the first post is "Water King", then in the water king to traverse the ID, you will encounter a challenge, you may be the number of posts will increase The next ID may also be "Water King", then how to do? Then compare the number of posts. If the next post is the same as the post, the number of posts increases by 1, skill +1, and if the number of the next post is not the same as the number of posts, then someone else may be the "Water King", and the number of their posts-1, two offset each other, to see who can survive to the end;because if you are "Water King", your number of posts must be greater than other users, so finally must be "water King" win. So traversing down, only the water King's post minus the opponent's number of posts will be greater than 0. No other post can be a fight with it.
Solution Three steps:
(1). Assume that the first ID of a post is the most frequently used candidate record, Ntimes represents the number of occurrences.
(2). Traverse the next ID, if the same as candidate, ntimes++, otherwise, encounter another opponent may also be "Water King", then ntimes--, if ntimes = = 0, the next step is to repeat the first steps.
(3). The end of the traversal, ntimes>0 the candidate is the "Water king" ID, he is the winner.

The specific implementation of the code:
int Find (int *arr,int lenght) {int candidate;int ntimes,i;for (i=ntimes=0;i<lenght;i++) {if (0==ntimes) {candidate= Arr[i];ntimes=1;} Else{if (Candidate==arr[i]) ntimes++;elsentimes--;} Return candidate;//candidate must be the number of the last ntimes of 1;}
extension issues: As Tango evolved, administrators found that "super Water King" was gone. Statistical results show that there are 3 posts a lot of IDs, their number of postings are more than the total number of post n 1/4. Can you quickly find their ID from the list of post IDs?
Although the topic has become three, but its nature has not changed, we can still continue the above ideas to solve the problem. Just now three "water King", in turn using the above method, we will soon find these three, because they have more than 1/4 posts.
The specific implementation code is as follows:

int candidate[3];void Find (int* arr, int lenght,int *candidate) {int ntimes[3];  ntimes[0]=ntimes[1]=ntimes[2]=0;  The number of times is initialized to 0 candidate[0]=candidate[1]=candidate[2]=0; Represents three water kings; for (int i = 0; i < length; i++) {if (arr[i]==candidate[0]) {ntimes[          0]++;          } else if (Arr[i]==candidate[1]) {ntimes[1]++;          } else if (Arr[i]==candidate[2]) {ntimes[2]++;               } else if (ntimes[0]==0) {ntimes[0]=1;          Candidate[0]=arr[i];               } else if (ntimes[1]==0) {ntimes[1]=1;          Candidate[1]=arr[i];               } else if (ntimes[2]==0) {ntimes[2]=1;          Candidate[2]=arr[i];               } else {ntimes[0]--;               ntimes[1]--;           ntimes[2]--;   }} return;
Because you only need to find the "Water king", so the idea is the same as above.



Looking for a post "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.