algorithm to find the three majority elements in an array

后端 未结 2 596
醉话见心
醉话见心 2020-12-28 10:32

Let\'s say there are three elements in a non-sorted array all of which appear more than one-fourth times of the total number of elements.

What is the most efficient

相关标签:
2条回答
  • 2020-12-28 10:49

    create a histogram of the entries, sort it, and take the three largest entries.

    0 讨论(0)
  • 2020-12-28 10:52

    Remember up to three elements, together with counters.

    1. remember first element, set count1 = 1
    2. scan until you find first different element, increasing count1 for each occurrence of element 1
    3. remember second elemt, set count2 = 1
    4. scan until you find first element different from elem1 and elem2, incrementing count1 or count2, depending which element you see
    5. remember third element, set count3 = 1
    6. continue scanning, if the element is one of the remembered, increment its count, if it's none of the remembered, decrement all three counts; if a count drops to 0, forget the element, go to step 1, 3, or 5, depending on how many elements you forget
    7. If you have three elements occurring strictly more than one-fourth times the number of elements in the array, you will end up with three remembered elements each with positive count, these are the three majority elements.

    Small constant additional space, O(n), no sorting.

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