Majority Number II

原题链接: http://lintcode.com/en/problem/majority-number-ii/#

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

SOLUTION 1:

与Majority Number 1相似。但我们要保存2个number.

1. 遇到第一个不同的值,先记录number 2.

2. 新值与n1,n2都不同,则cnt1,cnt2都减少

3. 当n1,n2任意一个为0时,从新值中挑出一个记录下来。

4. 最后再对2个候选值进行查验,得出最终的解。

主页君其实也想不太明白这个题目为什么这样解。

还是举个例子吧

7 1 7 7 61 61 61 10 10 10 61

n1      7 7 7 7  7   7   7   7   7 10 10

cnt1    1 1 2 3  2   2   2   1   0  1   1

n2       0 1 1 1  1  61  61 61 61 61 61

cnt2    0 1 1 1  0   1   2    1  0   0   1

证明:

1. 我们对cnt1,cnt2减数时,相当于丢弃了3个数字(当前数字,n1, n2)。也就是说,每一次丢弃数字,我们是丢弃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个数字呢?因为数字的编排可以让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
// When there are only 1 or 2 elements in the array,
// there is no solution.
if (nums == null || nums.size() <= 2) {
return -1;
} int n1 = 0;
int n2 = 0; int cnt1 = 0;
int cnt2 = 0; int size = nums.size();
for (int i = 0; i < size; i++) {
int num = nums.get(i);
if (cnt1 != 0 && num == n1) {
cnt1++;
} else if (cnt2 != 0 && num == n2) {
cnt2++;
} else if (cnt1 == 0) {
cnt1 = 1;
n1 = num;
} else if (cnt2 == 0) {
cnt2 = 1;
n2 = num;
} else {
cnt1--;
cnt2--;
}
} // count the two candiates.
cnt1 = 0;
cnt2 = 0;
for (int num: nums) {
if (num == n1) {
cnt1++;
} else if (num == n2) {
cnt2++;
}
} if (cnt1 < cnt2) {
return n2;
} return n1;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/math/MajorityNumber2.java

Lintcode: Majority Number II 解题报告的更多相关文章

  1. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  2. Lintcode: Majority Number II

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

  3. 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 ...

  4. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  5. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  6. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

  7. Lintcode: Majority Number III

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

  8. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

随机推荐

  1. 【基础】centos 6.X 下修改图形界面为命令行界面(单用户救援模式)

    1. Linux开机引导的时候,按键盘上的e 就可以进入进入GRUB菜单界面. 2.在出现GRUB引导画面时(CentOS(2.6.18-274**)),按字母e键,进入GRUB编辑状态: 3.把光标 ...

  2. excel中的数据粘贴不全到plsql中,excel 粘贴后空白,Excel复制粘贴内容不全

    http://zhidao.baidu.com/link?url=pHZQvfWJzI-lQjl4uP86q4GLcpYHu4o-fdjiYegJS0Cy5HEq5oz0YrUye3iHjmv5CJ3 ...

  3. Unix awk使用手册

    什么是awk? 你可能对UNIX比较熟悉,但你可能对awk很陌生,这一点也不奇怪,的确,与其优秀的功能相比,awk还远没达到它应有的知名度.awk是什么?与其它大多数UNIX命令不同的是,从名字上看, ...

  4. 【JQuery】jquery对象和javascript对象即DOM对象相互转换

    jQuery 对象是通过 jQuery 包装DOM 对象后产生的对象.jQuery 对象是 jQuery 独有的,其可以使用 jQuery 里的方法,但是不能使用 DOM 的方法:例如: $(&quo ...

  5. 【Java】详解java对象的序列化

    目录结构: contents structure [+] 序列化的含义和意义 使用对象流实现序列化 对象引用的序列化 自定义序列化 采用实现Serializable接口实现序列化 采用实现Extern ...

  6. MySQL慢查询mysqlsla

    转:http://www.osyunwei.com/archives/7659.html 必须在MySQL服务器上安装mysqlsla 1.mysqlsla是perl编写的脚本,运行mysqlsla需 ...

  7. Kubernetes(k8s) docker集群搭建

    原文地址:https://blog.csdn.net/real_myth/article/details/78719244 一.Kubernetes系列之介绍篇   •Kubernetes介绍 1.背 ...

  8. Xcode command line tools

    1.Xcode command line tools 安装 如果你不是一名 iOS 或 OS X 开发者,可以跳过安装 XCode 的过程,直接安装 Xcode command line tools. ...

  9. 【web】a标签点击时跳出确认框

    [web]a标签点击时跳出确认框 https://blog.csdn.net/michael_ouyang/article/details/52765575需求如下: 在跳转链接前,需要判断该用户是否 ...

  10. 转如何用九条命令在一分钟内检查Linux服务器性能?

    一.uptime命令 $ uptime :: up :,   user,  load average: 30.02, 26.43, 19.02 这个命令可以快速查看机器的负载情况.在Linux系统中, ...