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. Behavior-Based Intelligence

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Early work in artificial intelligenc ...

  2. 数据库CRUD操作:C:create创建(添加)、R:read读取、U:update:修改、D:delete删除;高级查询

    1.注释语法:--,#2.后缀是.sql的文件是数据库查询文件3.保存查询4.在数据库里面 列有个名字叫字段   行有个名字叫记录5.一条数据即为表的一行 CRUD操作:create 创建(添加)re ...

  3. 低功耗蓝牙4.0BLE编程-nrf51822开发(2)

    相关下载:http://download.csdn.net/detail/xgbing/9565708 首先看的示例是心率计一个示例程序:<KEIL path> \ARM\Device\N ...

  4. Android源码剖析之Framework层进阶版(Wms窗口管理)

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 上一篇我们主要讲了Ams,篇幅有限,本篇再讲讲Wms,即WindowManagerService,管 ...

  5. job_chain

    JOB链:JOB之间的相互触发操作. 实验意图:有些JOB具有先后调用次序,比如先做一件事情,这件事情做完才能继续下一件事情,而第一个JOB如果自己挂掉的话,第二个JOB需要正常运行(默认是终止),这 ...

  6. ArcGIS API for Silverlight 地图元素点闪烁,线流动显示的处理方式

    原文:ArcGIS API for Silverlight 地图元素点闪烁,线流动显示的处理方式 <Grid x:Name="LayoutRoot" Background=& ...

  7. {$ecs_css_path}

    includes里的init.php的187-194行 if (!empty($_CFG['stylename'])) { $smarty->assign('ecs_css_path', 'th ...

  8. 我的工具箱之Putty

    这是类似于SecureCrt的终端仿真软件,个人感觉比SecureCrt差一点. 下载地址是:http://pan.baidu.com/s/1sko0GrF SecureCrt网址在我的工具箱之Sec ...

  9. 解决IntelliJ Idea中文乱码问题

    乱码的根源是字符编码与解码不一致 解决之道:统一编码

  10. zabbix配置发送报警邮件

    配置邮件分为两种情况: 第一种:使用远端邮件服务器发送报警邮件 Linux系统版本:CentOS6.5-64 下载mailx: http://nchc.dl.sourceforge.net/proje ...