[LeetCode#169]Majority Element

本文介绍两种寻找数组中多数元素的方法:一是使用HashMap记录每个整数出现的次数;二是利用题目特性,多数元素出现次数超过n/2,通过遍历数组并比较元素实现。

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;
    }
}

 

转载于:https://www.cnblogs.com/airwindow/p/4271432.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值