Link: https://leetcode.com/problems/majority-element-ii/
Solution:
Topics: Boyer-Moore voting
Intuition
This is a very tricky twist on the classic Majority element problem. Basically the twist is now which elements appear at more than n/3
times. The key insight is understanding that there can be at most 2 elements that appear more than n/3
times. Why?
Because in the best case that n
is divisible by 3, you would have 3 partitions of exactly n/3
length. For an element to appear more than n/3
times, it would have to eat into at least one partition. So in the best case, you could have two homogenous partitions that can both eat into the third partition to reach n/3
elements.
But how do we modify the Majority element algorithm to work with n/3
? The idea is to use two candidates and two counts, but the key here is in disallowing them to overwrite one another or take the same value.
Implementation