ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 0169 Majority Element

2022-05-28 11:34:00  阅读:181  来源: 互联网

标签:0169 candidate majority int nums votes element Majority LeetCode


原题传送门

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)

标签:0169,candidate,majority,int,nums,votes,element,Majority,LeetCode
来源: https://www.cnblogs.com/junstat/p/16320140.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有