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

    The problem:

    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.

    Credits:
    Special thanks to @ts for adding this problem and creating all test cases.

    My analysis:

    This problem could be solved by two different ways.
    1. use a HashMap to record each integer's appearance. 
    to get the final target value, we could use two methods.
    1.1 scan through the HashMap to get the key of maximum appearance.
    1.2 use two varibles: target and max_count, to keep track of the max_count and target all the time.
    if (map.get(num[i]) > max_count) {
        target = num[i];
        max_count = map.get(num[i]);
    }
    
    2. use the property given by the problem: the majority element appears more than N/2 times.
    Since the majority element appears more than N/2 times, we could the following pattern:
    2.1. initialize the target as num[0] and count as 0.
    int target = num[0];
    int count = 1;
    
    2.2. once we encounter a new element, we compare it with target
    2.2.1  iff the new element = target, we increase the count by 1;and if the count larger than num.length/2(seldom), we could return the target directly.
    2.2.2  iff the new element != target, we decrease the count by 1;
    if (target != num[i]) {
        count--;
    } else{
        count++;
        if (count > num.length/2)
            return target;
    }
    
    The key idea behind this solutin is that: the majority appears more than N/2. 
    Even the array is not sorted, once we detect a inequality between two elements(not integer, the same integer could have multiple elements), we remove the two elements from the candidate list. Thus we could finally leave with the element with the most apperance.
    Note: once we exclude all elements ahead of num[i] (when count is equal to 0), we need to pick num[i] into the candidate list to continue the invariant:
    if (count == 0) {
        target = num[i];
        count = 1;
        continue;
    }
    
    Key: we have a condidate list(only a single element)!!!!
    All we care is the element's value!

    Solution1 (HashMap based):

    public class Solution {
        public int majorityElement(int[] num) {
            if (num == null || num.length == 0)
                return -1;
            int target = 0;
            int max_count = 0;
            HashMap<Integer, Integer> map = new HashMap<Integer, Integer> ();
            for (int i = 0; i < num.length; i++) {
                if (map.containsKey(num[i])) {
                    map.put(num[i], map.get(num[i])+1);
                } else{
                    map.put(num[i], 1);
                }
                if (map.get(num[i]) > max_count) {
                    target = num[i];
                    max_count = map.get(num[i]);
                }
            }
            return target;
        }
    }

    Solution2 (use the proerty of the maximum appearance larger than N/2)

    public class Solution {
        public int majorityElement(int[] num) {
            if (num == null || num.length == 0)
                return -1;
            int target = num[0];
            int count = 1;
            for (int i = 1; i < num.length; i++) {
                if (count == 0) {
                    target = num[i];
                    count = 1;
                    continue;
                }
                if (target != num[i]) {
                    count--;
                } else{
                    count++;
                    if (count > num.length/2)
                        return target;
                }
            } 
            return target;
        }
    }
  • 相关阅读:
    svn导出文件夹到另外目录export
    关键词提取自动摘要相关开源项目,自动化seo
    高级前端面试题,部分答案待补充
    三句话感受一本书,让脑子变聪明的7本书,每本只需理解3句话!
    关于系统设置分辨率比例影响到网页宽度大小的问题
    QQ在线客服,默认到要加好友,授权也不起作用需要先开通QQ营销服务
    网络营销相关缩写名称CPM CPT CPC CPA CPS SEM SEO解析
    让nodepad++编辑时链接能双击打开
    工作是一种修行,工作本身,就是一种修行(深度好文)
    foxmail收取163企业邮箱设置,不能直接用foxmail默认的配置,否则一直提示帐号密码错误
  • 原文地址:https://www.cnblogs.com/airwindow/p/4271432.html
Copyright © 2011-2022 走看看