zoukankan      html  css  js  c++  java
  • LeetCode Majority Element

     1 class Solution {
     2 public:
     3     int majorityElement(vector<int> &num) {
     4         int candidate = num[0];
     5         int count = 1;
     6         int len = num.size();
     7         
     8         for (int i=1; i<len; i++) {
     9             if (count == 0) {
    10                 candidate = num[i];
    11                 count++;
    12                 continue;
    13             }
    14             candidate == num[i] ? count++: count--;
    15         }
    16         return candidate;
    17     }
    18 };

    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.

      • Runtime: O(n2) — Brute force solution: Check each element if it is the majority element.
      • Runtime: O(n), Space: O(n) — Hash table: Maintain a hash table of the counts of each element, then find the most common one.
      • Runtime: O(n log n) — Sorting: As we know more than half of the array are elements of the same value, we can sort the array and all majority elements will be grouped into one contiguous chunk. Therefore, the middle (n/2th) element must also be the majority element.
      • Average runtime: O(n), Worst case runtime: Infinity — Randomization: Randomly pick an element and check if it is the majority element. If it is not, do the random pick again until you find the majority element. As the probability to pick the majority element is greater than 1/2, the expected number of attempts is < 2.
      • Runtime: O(n log n) — Divide and conquer: Divide the array into two halves, then find the majority element A in the first half and the majority element B in the second half. The global majority element must either be A or B. If A == B, then it automatically becomes the global majority element. If not, then both A and B are the candidates for the majority element, and it is suffice to check the count of occurrences for at most two candidates. The runtime complexity, T(n) = T(n/2) + 2n = O(n log n).
      • Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
        1. If the counter is 0, we set the current candidate to x and the counter to 1.
        2. If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.
        After one pass, the current candidate is the majority element. Runtime complexity = O(n).
      • Runtime: O(n) — Bit manipulation: We would need 32 iterations, each calculating the number of 1's for the ith bit of all n numbers. Since a majority must exist, therefore, either count of 1's > count of 0's or vice versa (but can never be equal). The majority number’s ith bit must be the one bit that has the greater count.

    Moore voting algorithm: http://www.cs.utexas.edu/~moore/best-ideas/mjrty/

  • 相关阅读:
    OC MRC之循环引用问题(代码分析)
    OC MRC之 @property参数(代码分析)
    OC MRC之set方法内存管理(代码分析)
    OC MRC之多对象之间管理(代码分析)
    OC MRC之计数器的基本操作(代码分析)
    最流行的android组件大全
    Android主题切换方案总结
    Picasso – Android系统的图片下载和缓存类库
    Android无法导入下载好的项目(和Eclipse中已经存在的项目命名一样导致冲突)解决办法
    Android Studio 简单设置
  • 原文地址:https://www.cnblogs.com/lailailai/p/4440364.html
Copyright © 2011-2022 走看看