zoukankan      html  css  js  c++  java
  • [LeetCode 169] Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

    You may assume that the array is non-empty and the majority element always exist in the array.

    Example 1:

    Input: [3,2,3]
    Output: 3

    Example 2:

    Input: [2,2,1,1,1,2,2]
    Output: 2

     

     

    The sorting solution or O(N) space solution using hash map is trivial. There is another O(N) algorithm called Boyer-Moore Majority Vote algorithm that only uses O(1) space. We need two passes over the input array. In the 1st pass, we generate a single candidate value which is the majority value if there is a majority. The 2nd pass simply counts the frequency of that value to confirm.

    In the 1st pass, we need 2 values: a candidate value, initially set to any value; a count, initially set to 0. Then for each element, do the following.

    1. if current element == candidate, count++;

    2. else if current element != candidate: if count is 0, set current element as the new candidate and count to 1; if count is > 0, count--;

    Proof of correctness:  

    1. if we first pick a majority number as candidate, then say after consuming k numbers its count is decreased to 0, this means we've consumed k / 2 majority numbers and k / 2 non-majority numbers. The remaining array has > n / 2 - k / 2 majority numbers and < n - k - n / 2 + k / 2 = n / 2 - k / 2 non-majority numbers. The majority number is still the majority number. 

    2. if we first pick a non-majority number C as candidate, then say after consuming k numbers its count is decreased to 0, we've consumed at most k / 2 majority numbers to de-throne C's candidancy. This means the remaining array has > n / 2 - k / 2 majority numbers and < n - k - n / 2 + k / 2 = n / 2 - k / 2 non-majority numbers. Exactly the same with case 1.

    For a more detailed explanation, refer to Majority Voting Algorithm.

    class Solution {
        public int majorityElement(int[] nums) {
            int candidate = nums[0], cnt = 1;
            for(int i = 1; i < nums.length; i++) {
                if(nums[i] == candidate) {
                    cnt++;
                }
                else {
                    if(cnt == 0) {
                        candidate = nums[i];
                        cnt = 1;
                    }
                    else {
                        cnt--;
                    }
                }
            }
            // cnt = 0;
            // for(int i = 0; i < nums.length; i++) {
            //     if(nums[i] == candidate) {
            //         cnt++;
            //     }
            // }
            // return cnt > nums.length / 2 ? candidate : -1;
            return candidate;
        }
    }

     

     

    Related Problems

    Majority Element II

    Majority Element III

    Single Number 

    Single Number II

    Single Number III

     

  • 相关阅读:
    数字图像处理领域算法之高斯平滑
    字符串数组元素反转
    安装visual studio 2008 team 失败
    .NET中的正则表达式 (二)RegexOptions 枚举
    手动备份、还原windows7、office2010激活信息
    [转]蓝牙基带数据传输机理分析
    .NET中的正则表达式 (三)RegexCompilationInfo 类
    Android AVD语言设置
    蓝牙协议栈
    Bluetooth StructureBlueZ
  • 原文地址:https://www.cnblogs.com/lz87/p/7204855.html
Copyright © 2011-2022 走看看