[169] LeetCode find the mode - Most elements

Topic
Here Insert Picture Description
ideas
I believe in statistics to see the number of times when we may think of the algorithm is similar to bucket sort, yes, but the bucket, we need a hash table to achieve, because we only count the number of required elements in the array appears.
So we create a hash table, the Value used to count when the bucket, so we loop through the array,
to determine whether this element is already Key, and if it is, then put (Key, get () + 1) at this time on this element count + 1;
if not then put (Key, 1);

After we create a hash table, we traverse the hash table, the Key maximum number of nodes return to Value.
The time complexity of O (n);
space complexity O (k); k is the number of digits appears array, we created a small hash table K

  public static int majorityElement(int[] nums) {
    if(nums.length < 1 || nums == null) return -1;
    if(nums.length < 3) return nums[0];

    HashMap hashmap = new HashMap<Integer,Integer>();
    hashmap.put(nums[0],0);
    for(int i = 1 ; i < nums.length; i++ ){
        if(hashmap.containsKey(nums[i])){
            int Value = (int)hashmap.get(nums[i]);
            hashmap.put(nums[i], ++Value);
            continue;
        }
        hashmap.put(nums[i],0);
    }
    Map.Entry<Integer, Integer> majorityentry = null;
    Iterator it = hashmap.entrySet().iterator();
    while(it.hasNext()){
        Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>)it.next();

        if (majorityentry == null || entry.getValue() > majorityentry.getValue()) {
            majorityentry = entry;
        }

    }

    return majorityentry.getKey();

}

The official solution given five species, of which the referendum law made me scared, we can refer to.

Method 6: Boyer-Moore algorithm vote
idea

If we take the mode denoted by + 1 + 1, the number recorded as other -1-1, all of them together, and obviously greater than 0, we can see from the results themselves superior number than any other number.

algorithm

Essentially, Boyer-Moore algorithm is to find nums a suffix sufsuf, which suf [0] suf [0] is the number of public suffix. We maintain a counter, if you encounter a number of our current candidates for the public, it will add a counter, or minus one. As long as the counter is equal to 0, we will access the digital nums before all forget, and the next number as the number of minority candidates. Intuitively this algorithm is not obvious is why, we look at the following example (vertical lines for each division of the counter to zero)

[7, 7, 5, 7, 5, 1 | 5, 7 | 5, 5, 7, 7 | 7, 7, 7, 7]

First, the first subscript 7 is a candidate as the mode 0. 5 is a subscript, the counter changes back to 0. 6 so subscript 5 is the next candidate of the mode. For this example, the number 7 is the real public, so ignored by previous figures, we ignored as many as the number of public and non-public purpose number. Therefore, the number 7 is still all the remaining numbers.

[7, 7, 5, 7, 5, 1 | 5, 7 | 5, 5, 7, 7 | 5, 5, 5, 5]

Now, the mode is 5 (when the counter reaches zero, we became a candidate from 7 5). At this point, we are not really candidates for the mode, but we forget in front of the digital time, to remove the same number of number of public and non-public number (if forgotten more non-public number, causes the counter to become negative number).

Therefore, the above process shows that we can safely forget the previous number and the number continues to solve all the remaining numbers. Finally, there is always a counter suffix is ​​greater than 0 satisfying, in which case the mode is the mode suffix entire array.

class Solution {
    public int majorityElement(int[] nums) {
        int count = 0;
        Integer candidate = null;

        for (int num : nums) {
            if (count == 0) {
                candidate = num;
            }
            count += (num == candidate) ? 1 : -1;
        }

        return candidate;
    }
}

Time complexity: O (n) O (n)

Boyer-Moore algorithm strictly enforced nn cycles, so the time complexity is linear time.

Space complexity: O (1) O (1)

Boyer-Moore just need extra space constant level.

Author: LeetCode link

Published 54 original articles · won praise 18 · views 2609

Guess you like

Origin blog.csdn.net/weixin_44916741/article/details/104032759