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. 【转载】Linux系统与性能监控

    原文地址:http://kerrigan.sinaapp.com/post-7.html Linux System and Performance Monitoring http://www.hous ...

  2. Mac 下 Nginx、MySQL、PHP-FPM 的安装配置

    用了3年多的本本罢工,最近新入手了一台 rmbp,一堆工作环境要配置,LNMP 里的 NMP 是常规要安装的,恰好也是第一次在 mac 上安装配置 nginx.mysql.php,所以顺便做个记录,免 ...

  3. Andrew Ng机器学习公开课笔记–Reinforcement Learning and Control

    网易公开课,第16课 notes,12 前面的supervised learning,对于一个指定的x可以明确告诉你,正确的y是什么 但某些sequential decision making问题,比 ...

  4. 【Demo】微信上墙

    先看看微信墙效果图 使用简单说明 关于微信公众号 回复 "上墙",点击授权文章进行授权 回复"#上墙内容" 即可发表上墙消息了 查看微信墙列表,点击这里 原文地 ...

  5. php 连接主从数据库

    本代码是从uchome的代码修改的,是因为要解决uchome的效率而处理的.这个思维其实很久就有了,只是一直没有去做,相信也有人有同样的想法,如果有类似的,那真的希望提出相关的建议.封装的方式比较简单 ...

  6. JQuery源码之“对象的结构解析”

    吃完午饭,觉得有点发困,想起了以后我们的产品可能要做到各种浏览器的兼容于是乎不得不清醒起来!我们的web项目多数是依赖于Jquery的.据了解,在Jquery的2.0版本以后对IE的低端版本浏览器不再 ...

  7. iOS 获取当前时间 年、月、日、周几

    NSDate * nowDate = [NSDate new]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger uni ...

  8. Ubuntu下面安装eclipse for c++

    1.下载jdk,cdt,eclipse. 其中jdk是java运行环境,eclipse需要先装jre,才可能运行,cdt是在eclipse中运行c\c++程序的插件.jdk安装见上文. 2.eclip ...

  9. [收藏夹整理]VC部分

    c++多线程(一) C++11 多线程 鸡啄米:C++编程入门系列之十二(类与对象:面向对象设计的基本思想和特点) 鸡啄米:C++编程入门系列之二十一(C++程序设计必知:类的静态成员) [笔记]VS ...

  10. iOS基础

    1.属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作用,在那种情况下用? readwrite 是可读可写特性:需要生成getter方法和s ...