1. 程式人生 > 其它 >LeetCode 0169 Majority Element

LeetCode 0169 Majority Element

原題傳送門

1. 題目描述

2. Solution 1

1、思路分析
Hash Table
Count the number of appearances for each distinct number in nums, once we see a number appear more than n / 2
times, it is the majority element.

2、程式碼實現

package Q0199.Q0169MajorityElement;

import java.util.HashMap;
import java.util.Map;

/*
    Hash Table
    Count the number of appearances for each distinct number in nums, once we see a number appear more than n / 2
    times, it is the majority element.
 */
public class Solution1 {
    public int majorityElement(int[] nums) {
        Map<Integer, Integer> counter = new HashMap<>();
        for (int num : nums) {
            if (counter.containsKey(num))
                counter.put(num, counter.get(num) + 1);
            else
                counter.put(num, 1);

            if (counter.get(num) > nums.length / 2)
                return num;
        }
        return 0;
    }
}

3、複雜度分析
時間複雜度: O(n)
空間複雜度: O(n)

3. Solution 2

1、思路分析
Sorting

2、程式碼實現

package Q0199.Q0169MajorityElement;

import java.util.Arrays;

/*
    Sorting
 */
public class Solution2 {
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length / 2];
    }
}

3、複雜度分析


時間複雜度: O(n logn)
空間複雜度: O(1)

4. Solution 3

1、思路分析
Boyer-Moore Majority Voting Algorithm
Reference: https://www.geeksforgeeks.org/boyer-moore-majority-voting-algorithm/

The Boyer-Moore voting algorithm is one of the popular optimal algorithms which is used to find the majority element among the given elements that have more than N/ 2 occurrences. This works perfectly fine for finding the majority element which takes 2 traversals over the given elements, which works in O(N) time complexity and O(1) space complexity.

Intuition Behind Working :
When the elements are the same as the candidate element, votes are incremented when some other element is found not equal to the candidate element. We decreased the count. This actually means that we are decreasing the priority of winning ability of the selected candidate, since we know that if the candidate is a majority it occurs more than N/2 times and the remaining elements are less than N/2. We keep decreasing the votes since we found some different element than the candidate element. When votes become 0, this actually means that there are the same number of different elements, which should not be the case for the element to be the majority element. So the candidate element cannot be the majority, so we choose the present element as the candidate and continue the same till all the elements get finished. The final candidate would be our majority element. We check using the 2nd traversal to see whether its count is greater than N/2. If it is true, we consider it as the majority element.

Steps to implement the algorithm :
Step 1 – Find a candidate with the majority –

Initialize a variable say i ,votes = 0, candidate =-1
Traverse through the array using for loop
If votes = 0, choose the candidate = arr[i] , make votes=1.
else if the current element is the same as the candidate increment votes
else decrement votes.
Step 2 – Check if the candidate has more than N/2 votes –

Initialize a variable count =0 and increment count if it is the same as the candidate.
If the count is >N/2, return the candidate.
else return -1.

2、程式碼實現

/*
    Boyer-Moore Majority Voting Algorithm
 */
public class Solution3 {
    public int majorityElement(int[] nums) {
        int votes = 0, candidate = 0;
        for (int num : nums) {
            if (votes == 0) candidate = num;
            votes += num == candidate ? 1 : -1;
        }
        return candidate;
    }
}

3、複雜度分析
時間複雜度: O(n)
空間複雜度: O(1)

5. Solution 4

1、思路分析
Bit Manipulation
The bits in the majority are just the majority bits of all numbers.

2、程式碼實現

package Q0199.Q0169MajorityElement;

/*
    Bit Manipulation
    The bits in the majority are just the majority bits of all numbers.
 */
public class Solution4 {
    public int majorityElement(int[] nums) {
        int[] bit = new int[32];
        for (int num : nums)
            for (int i = 0; i < 32; i++)
                if ((num >> (31 - i) & 1) == 1) bit[i]++;

        int ret = 0;
        for (int i = 0; i < 32; i++) {
            bit[i] = bit[i] > nums.length / 2 ? 1 : 0;
            ret += bit[i] * (1 << (31 - i));
        }
        return ret;
    }
}

3、複雜度分析
時間複雜度: O(n)
空間複雜度: O(1)