Algorithm (7) Majority Element II

Source: Internet
Author: User

title : Find the number of occurrences in the array greater than n/3 times

idea : Moore vote method. All the posts say: Go through the array first to find the alternative elements, and then go through the array to see if the element is really more than N/3, and then directly on the code, but now the question is: How do I find this alternative element?! How did the tickets get thrown? By referring to the code in the article, it is generally understood that clairvoyant (http://www.cnblogs.com/grandyang/p/4606822.html). So how do you iterate through the array and find the alternate elements? The algorithm also has to find more than half of the she used: The entire array has two different numbers, the two same number is cleared out , so that the last array will definitely leave some number! This is the process of voting , and then go through the array, check the selected tickets on it! So when it corresponds to N/3,N/4, will such a situation exist? Corresponds to three number of cases, the array has a maximum of 2 number of occurrences more than N/3, then if there is only one such number? Like [7,7,7,5,4,6,8,10] like this? First 777 into the stack, if it is also the current stack of DSLR is not the same, then such two numbers will be out of the stack, so, then when the 5,4,6 attack, 7 is definitely pass off, this is greater than N/2 situation analysis, then N/3 situation analysis? It should be said that the preparation of two stacks, two stacks are stored in the current two candidates, and then each time the challenger came, respectively, the two stacks in comparison with the numbers, if different, then two stacks out of one! "Here's a small detail, when a stack in two stacks is empty, then the challenger occupies the stack." So, in the same way, when we find a number greater than N/4, we have to prepare 3 stacks ( because at most 3 elements ), if which stack is empty, the Challenger occupies an idle stack, otherwise, if the Challenger is not equal to any one of the elements in the stack, the elements in the stack will have to pop up! When programming, be careful to handle this "stack"!

Answer :

Algorithm (7) Majority Element II

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.