Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.

Find it.

Note
There is only one majority number in the array Example
For [1, 2, 1, 2, 1, 3, 3] return 1 Challenge
O(n) time and O(1) space

三三抵销法,但是也有需要注意的地方:

1. 我们对cnt1,cnt2减数时,相当于丢弃了3个数字(当前数字,candidate1, candidate2)。也就是说,每一次丢弃数字,我们是丢弃3个不同的数字。

而Majority number超过了1/3所以它最后一定会留下来。

设定总数为N, majority number次数为m。丢弃的次数是x。则majority 被扔的次数是x

而m > N/3, N - 3x > 0.

3m > N,  N > 3x 所以 3m > 3x, m > x 也就是说 m一定没有被扔完

最坏的情况,Majority number每次都被扔掉了,但它一定会在n1,n2中。

2. 为什么最后要再检查2个数字呢(从头开始统计,而不用剩下的count1, count2)?因为数字的编排可以让majority 数被过度消耗,使其计数反而小于n2,或者等于n2.前面举的例子即是。

另一个例子:

1 1 1 1 2 3 2 3 4 4 4 这个 1就会被消耗过多,最后余下的反而比4少。

 public class Solution {
/**
* @param nums: A list of integers
* @return: The majority number that occurs more than 1/3
*/
public int majorityNumber(ArrayList<Integer> nums) {
// write your code
int candidate1 = 0;
int candidate2 = 0;
int count1 = 0;
int count2 = 0;
for (int elem : nums) {
if (count1 == 0) {
candidate1 = elem;
}
if (count2 == 0 && elem != candidate1) {
candidate2 = elem;
}
if (candidate1 == elem) {
count1++;
}
if (candidate2 == elem) {
count2++;
}
if (candidate1 != elem && candidate2 != elem) {
count1--;
count2--;
}
} count1 = 0;
count2 = 0;
for (int elem : nums) {
if (elem == candidate1) count1++;
else if (elem == candidate2) count2++;
}
return count1>count2? candidate1 : candidate2;
}
}

Lintcode: Majority Number II的更多相关文章

  1. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

  2. LintCode Majority Number II / III

    Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...

  3. Lintcode: Majority Number III

    Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...

  4. lintcode 中等题:Majority number II 主元素 II

    题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...

  5. LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III

    LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...

  6. Lintcode: Majority Number 解题报告

    Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...

  7. [LintCode] Majority Number 求众数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  8. [LintCode] Majority Number 求大多数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  9. LintCode: Single Number II

    一篇解析比较详细的文章:http://www.acmerblog.com/leetcode-single-number-ii-5394.html C++ 解法(1) 求出每个比特位的数目,然后%3,如 ...

随机推荐

  1. Introducing the Accelerated Mobile Pages Project, for a faster, open mobile web

    https://googleblog.blogspot.com/2015/10/introducing-accelerated-mobile-pages.html October 7, 2015 Sm ...

  2. hot code replace

    http://wiki.eclipse.org/FAQ_What_is_hot_code_replace%3F https://social.msdn.microsoft.com/Forums/vst ...

  3. delphi 向其他程序发送模拟按键

    向其他程序发送模拟按键: 1.用keybd_event: varh : THandle;beginh := FindWindow('TFitForm', '1stOpt - [Untitled1]') ...

  4. duplicate symbol _OBJC_CLASS 错误处理方法

    错误: ld: duplicate symbol _OBJC_CLASS_$_************ in **************** 一种可能性是你的项目的不同group里有着相同名称的类 ...

  5. Mongo简单查询总结

    mongo 简单查询db.ansheng.findOne()//返回第一条匹配到的数据db.ansheng.findOne({"aaaaa":4})db.ansheng.find( ...

  6. DOCTYPE、指定语言、字符集

    <!DOCTYPE html> 在HTML的最开始部分声明DOCTYPE文档类型,可以让浏览器或其他用户代理知道你要使用的Html语言类型:无论你打算使用何种类型的HTML语言,DOCTY ...

  7. Maven实战(一)安装与配置

    1. 简介 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 如果你已经有十次输入同样的Ant targets来编译你的代码.jar或者w ...

  8. 1001 数组中和等于K的数对

    1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A ...

  9. java用spring实现文件下载

    今天是我第一博客文章,希望写出来的东西能让大家看明白,欢迎大家给我留言. html页面: <a href="#" onclick="downLoad()" ...

  10. PMP--可能会涉及到的计算题

    一.进度管理里的历时三点估算历时的三点估算可能会出现在进度管理的计算题里.以下公式,大家要记住:说一下历时的三点估算中的几个值:1.最有可能的历时估算:Tm2.最乐观的历时估算: To3.最悲观的历时 ...