Beauty of programming-poster

Source: Internet
Author: User

Question:

Tango is an experimental project of Microsoft Asia Research Institute. Employees and interns of the institute like to communicate with each other on Tango. Legend has it that Tango has a big "Water King" who not only likes to post, but also restores each post sent by other IDs. It is widely reported that the number of posts by the "Water King" has exceeded half of 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 the legendary Tango Water King?

Analysis:

The most direct method. We can sort all IDs. Then, scan the list of sorted IDs to count the number of occurrences of each ID. If an ID appears more than half of the total number of times, the ID is output. The time complexity of this algorithm is O (n * log2n + n ).

If the ID list is already ordered, do you still need to scan the entire list to count the number of occurrences of each ID?

If an ID appears more than half of the total number of times. Then, no matter what the ID of the Water King is, the n/2 (numbered from 0) in the ordered ID list must be this ID. You do not have to scan the list again. If you can quickly locate an item in the list, except for the time complexity of sorting, the time required for post-processing is O (1 ).

The above two methods both need to sort the ID list first, and there is no essential improvement in time complexity. Can sorting be avoided?

If two different IDs are deleted each time (whether or not they contain the "Water King" ID), the number of occurrences of the "Water King" ID in the remaining ID list still exceeds half of the total number. By repeating this process, you can reduce the total number of IDs in the ID List (converted to a smaller question) to obtain the answer. This approach avoids the time-consuming steps of sorting. The total time complexity is only O (n), and only the extra memory of the constant is required. The Code is as follows;

public static int FindKingID (int [] ID,int N)    {        int BlogKing = 0;        int nTimes,i;                for(i = nTimes= 0;i<N;i++)        {            if(nTimes == 0)            {                BlogKing = ID[i];                nTimes = 1;            }            else            {                if(BlogKing == ID[i])                {                    nTimes++;                }                else {                    nTimes--;                }            }        }        return BlogKing;    }

This question embodies a common idea in computer science, that is, how to turn a problem into a number of small subproblems. Grouping, recursion, and greed are all based on this idea. In the conversion process, small problems are essentially the same as the original problems. We can also turn small problems into smaller problems. Therefore, the conversion process is very important. Like the above question, we have ensured that the solution of the problem is still of the same nature as that of the original problem:

The number of IDs in the ID list is more than half. The higher the conversion efficiency, the faster the problem scale down after the conversion, the lower the overall time complexity.

Beauty of programming-poster

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.