• 28.0%

https://leetcode.com/problems/majority-element-ii/description/

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Hint:

• How many majority elements could it possibly have?
• Do you have a better hint? Suggest it!

https://discuss.leetcode.com/topic/17564/boyer-moore-majority-vote-algorithm-and-my-elaboration

Boyer-Moore Majority Vote algorithm and my elaboration

For those who aren’t familiar with Boyer-Moore Majority Vote algorithm,
I found a great article (http://goo.gl/64Nams) that helps me to understand this fantastic algorithm!!
Please check it out!

The essential concepts is you keep a counter for the majority number X. If you find a number Y that is not X, the current counter should deduce 1. The reason is that if there is 5 X and 4 Y, there would be one (5-4) more X than Y. This could be explained as “4 X being paired out by 4 Y”.

And since the requirement is finding the majority for more than ceiling of [n/3], the answer would be less than or equal to two numbers.
So we can modify the algorithm to maintain two counters for two majorities.

Followings are my sample Python code:

https://discuss.leetcode.com/topic/17396/boyer-moore-majority-vote-algorithm-generalization

Boyer-Moore Majority Vote algorithm generalization

Boyer-Moore Majority Vote algorithm generalization to elements appear more than floor(n/k) times

https://discuss.leetcode.com/topic/17721/my-c-solution

My C++ Solution

https://discuss.leetcode.com/topic/23689/my-o-n-time-solution-20ms/3

My O(n) time solution ,20ms

My idea comes from Majority Vote algroithm,http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html.Now we vote two numbers simultaneously. if the next number is differents from them both.then the two numbers’ votes minus one. If some number’s vote comes zero,then vote the next number.Every time vote minus,it is the same that we remove the three numbers from the array.And the majority elemnts of original still are the majority elements in the end. So check t1 and t2 are the majority elements of original array at last.

https://discuss.leetcode.com/topic/17409/6-lines-general-case-o-n-time-and-o-k-space

6 lines, general case O(N) time and O(k) space

Solution

I keep up to two candidates in my counter, so this fulfills the O(N) time and O(1) space requirements.

Explanation

Think of it this way: Find three different votes and hide them. Repeat until there aren’t three different votes left. A number that originally had more than one third of the votes now still has at least one vote, because to hide all of its votes you would’ve had to hide more than three times one third of the votes - more votes than there were. You can easily have false positives, though, so in the end check whether the remaining up to two candidates actually had more than one third of the votes.

My code does just that: Collect (count) the votes for every number, but remove triples of three different votes on the fly, as soon as we have such a triple.

Generalization to ⌊N/k⌋, still O(N) time but O(k) space

For the general problem, looking for elements appearing more than ⌊N/k⌋ times for some positive integer k, I just have to change my 3 to k. Then it already works and takes takes O(k) space and O(kN) time.

The O(kN) time does not come from the main loop, though. Yes, each ctr -= … does cost k, but I only have to do it at most N/k times. To put it in terms of the above explanation, I can’t hide a vote more than once.

No, the culprit is my last line, counting each remaining candidate separately. If I count them at the same time, I get O(N) again. Here’s the full generalized code:

https://discuss.leetcode.com/topic/17473/c-solution-for-elements-appear-more-than-floor-n-k-times

C++ solution for elements appear more than floor(n/k) times