Linear time majority algorithm?

前端 未结 7 1352
深忆病人
深忆病人 2020-12-05 01:03

Can anyone think of a linear time algorithm for determining a majority element in a list of elements? The algorithm should use O(1) space.

If n is the s

相关标签:
7条回答
  • 2020-12-05 01:57

    This is a popular challenge question, and the answer is that it's impossible. The language of strings with majority elements is not regular (this is easily proven by the pumping lemma) so there is no way it can be recognized in constant space.

    Of course the trick is that you need a counter variable which takes O(log n) space, but since n is boundd by 2^32 or 2^64 and your computer is really a finite state machine with ~8^(ramsize+hddsize) states, everything is O(1).

    0 讨论(0)
提交回复
热议问题