Main elements looking for a post water king

Source: Internet
Author: User

The topic is described as: "Water king" post more than half of all posts, with the author ID of each post, beg the water king's ID

Two ingenious solutions to the beauty of programming

Solution One: id sort, then the N/2 item in the ID list is the ID to find (also order, time complexity O (NLOGN))

Solution two: By looking, each time from the list to remove two different ID, the last can be obtained this ID, time complexity o (N). There is also skill in writing, do not have to find a different in the continuation, if the next, then use a variable record this number, the number of times +1, meet the different-1. For example 1,1,2,3,4 Initial value =1,count = 1, Second 1,value = 1,count = 2, Next 2,value=1,count=1, next 3,value=3,count=0, next 4,value=4, Count=1 ... Until the end, look at value, which is the ID you are looking for.

#include <iostream>using namespace std;/*  Name: Find post "Water king"   Copyright:   Author:   DATE:02/04/13 21:11  Description: Each time you delete two different IDs (whether or not they contain "water king" id), then "Water king" ID occurrences are still more than half of the total number of  repeated this process, time complexity O (n) */int find (int id[ ], int n) {    int ntimes = 0, I, candidate;         for (i = 0; i < n;i++)    {          if (Ntimes = = 0)          {              candidate = id[i];              Ntimes = 1;          }          else          {              if (candidate = = Id[i])              {                  ntimes++;              }              else              {                  ntimes--;}}    }    return candidate;} int main ()  {      int arr[] = {1, 2, 3, 4, 2, 5, 2, 2, 3, 2, 5, 2};      printf ("Over Harf ID is%d\n", find (arr, n));      System ("pause");    return 0;  }  

Iterate over the values as follows:

I 0 1 2 3 4 5 6 7 8 9 10 11
Candidate 1 3 2 2
Ntimes 1 0 1 0 1 0 1 7 1 2 1 2

This algorithm has a drawback, do not know exactly how many times this ID appears, only to know that the number of occurrences of this ID is greater than N/2. If you want to know, at the end of the past, in the huge amount of data, the cost of more traversal is to be considered.

Solution Three: This problem can also be used hash statistics, in the identification of the ID, the time complexity of O (n), but the spatial complexity of two larger than the solution, the worst possible O (N) and the solution of two O (1).

Under the expansion of the problem ~

For the solution of a, if it is known that there are two posts in the amount of more than 1/3 ID, can be sorted, in the N/3,2N/3 place to take a number, in the statistics of the number of occurrences of these two, can be judged, time complexity O (NLOGN) +o (N) or O (NLOGN)

For solution two, the same above problem, then value and count can be recorded with a two-dimensional array, the same time complexity is O (N)

In the expansion of the problem, the amount of data

Solution one becomes a "huge number of data to find the median problem", and do not have all the sort (not a huge amount of data can also be used to find the median method) ~ So, the use of the characteristics of the number, each integer is 32 bits of binary numbers, according to the highest number of the order of the block, (After the end of the block is the size of the order) statistics of the number of each block, you can determine the median in which block, in the calculation of that block, will eventually find the median.

For the solution two, can also be recursive, the precondition must be known to have M posts in 1/(m+1) More than id~ if you want to save more than the number of words, you can use Hash-map instead of the array

For the solution of three, such a situation if the number of M is still not read into memory, the first to hash the ID, in the hash group, in the HASH-MAP statistics can be read into memory once HASH-MAP statistics can be, in Hash-map to find the value of the match. Compared with solution Two, there is only some decrease in the complexity of the space.

For the solution of the second idea of "reducing the size of the problem, to maintain the original nature of the problem," for the original topic, the nature of the water King's ID in the small problem is still more than half of the total number of posts. So the key to the problem is the answer, what is the key nature of the problem, and how to reduce the size of the problem. For the nature of the problem, then is the specific problem of specific analysis, and reduce the scale, or the sub-problem, the idea of dynamic planning, division of the law is useful, a lot of the problem is determined by the nature.

Finally, there is an extension problem, the title is as follows:
With the development of Tango, the administrator found that "super Water King" did not. 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?

The title only needs a result, and now requires 3 results, the ntimes used in the title, should also be changed to 3 counters. Now we need 3 variables to record the 3 different IDs currently traversed, and the Ntimes 3 elements correspond to the number of 3 IDs currently traversed. If there is an ID in the traversal that differs from these 3 current IDs, we determine whether the current 3 ID has a ntimes of 0, and if so, the new traversal ID is replaced with 1 for its traversal number (that is, ntimes minus 1), if the current 3 ID ntimes is not 0, The ntimes of the 3 IDs is subtracted by 1.

#include <iostream>using namespace Std;int candidate[3];int count[3] = {0};int Input[100];int num = 0;int Main () {    cout<< "Please input" <<endl;    int t;        while (cin>>t) {if (t = =-1) break;    input[num++] = t;    } bool flag = FALSE;        for (int i = 0;i < num;i++) {flag = false;            for (int j = 0;j < 3;j++) {if (count[j] = = 0) {continue;                } if (candidate[j] = = Input[i]) {count[j]++;            Flag = true;        }} if (flag = = true) {continue; } for (int j = 0;j < 3;j++) {if (count[j] = = 0) {Candidate[j] = in                Put[i];                count[j]++;                Flag = true;            Break        }} if (flag = = true) {continue;  } for (int j = 0;j < 3;j++)      {count[j]--;    }} cout<<count[0]<< "" <<count[1]<< "" <<count[2]<<endl; cout<<candidate[0]<< "<<candidate[1]<<" "&LT;&LT;CANDIDATE[2]&LT;&LT;ENDL;}

Main elements 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.