• [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

     

  • 相关阅读:
    React生命周期函数
    云效创建项目应用以及流水线的说明文档
    前端工作规范
    阮一峰 前端系列教程
    js对时间戳的处理 获取时间,昨天,今天,明天,时间不同格式
    当天时间小案例--时间戳,获取年月日星期时分秒
    React中构造函数constractor,为什么要用super(props)
    Java8新特性——Optional类的使用(有效的避免空指针异常)
    Java8新特性——新一套时间API的使用
    Java8新特性——StreamAPI 的使用
  • 原文地址:https://www.cnblogs.com/lz87/p/7204855.html
Copyright © 2020-2023  润新知