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. (转)失败和拒绝,也是一种肯定 找工作时,我四处碰壁这一段经历对自己职业生涯的帮助最大。为什么? "因为这些挫折让我的脸皮变厚了 如果你不是每天被人拒绝,那就说明你的人生目标不够远大 所谓成功,就是不停地经历失败,并且始终保持热情

    (转)失败和拒绝,也是一种肯定 昨天,先是看到一个老外,说了一句很震撼的话. "你个人的项目,应该有四分之一会失败,否则就说明你的冒险精神不够." (Expect and hope ...

  2. POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  3. ASP.NET MVC同时支持web与webapi模式

    原文地址:https://blog.csdn.net/laymat/article/details/65444701 我们在创建 web mvc项目时是不支持web api的接口方式访问的,所以我们需 ...

  4. 收缩sqlserver事务日志

    若要允许 DBCC SHRINKFILE 命令收缩文件,首先需要通过将数据库恢复模式设置为 SIMPLE 来截断该文件. 示例,收缩数据库abce的事务日志 USE abce; GO -- Trunc ...

  5. PopUpWindow使用详解(二)——进阶及答疑

      相关文章:1.<PopUpWindow使用详解(一)——基本使用>2.<PopUpWindow使用详解(二)——进阶及答疑> 上篇为大家基本讲述了有关PopupWindow ...

  6. wireshark的拆包与合并

    背景:分析较高并发情景下的通话质量不佳的原因,需要长期抓包. 一.自动打包 1. 指定以1MB的大小打包,这个必须在/var/tmp/目录下执行. tcpdump -i ens32 -vvvv -C ...

  7. IE6-IE9兼容性问题列表及解决办法:锁表头的JQuery方案和非JQuery方案(不支持IE6,7,8)

    鉴于从IE8开始,IE不再支持css的expression了,所以以前依靠它完成锁表头的代码就全部失效了,面对新的浏览器,一切又要重新来过了. 现在所能找到的对于锁表头的方案主要有两种路子:一种是使用 ...

  8. android 中 viewpager 滑动的指示器

    先看下效果图: 这个需要用到1个开源的 库,这个后面也会说下的. 工程目录: 1. MainActivity.java public class MainActivity extends Fragme ...

  9. MySQL设置从库只读模式

    常见现象 运维工作中会经常维护MySQL主从服务器,当然Slave我们只是用于读操作. 一般权限开通也只授权只读账号,但是有时候维护工作可能不是一个人在做,你不能保证其他同事都按照这个标准操作. 有同 ...

  10. 配置eureka 老是报错connected time out 或者 refused connected

    报错信息总是连接错误,我指定了端口号,却不按照我指定的端口进行访问,而是访问eureka-server 的端口号是8761 ,这是因为配置有问题. 查看 类 EurekaClientConfigBea ...