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. python中对两个 list 求交集,并集和差集

    python中对两个 list 求交集,并集和差集: 1.首先是较为浅白的做法: >>> a=[1,2,3,4,5,6,7,8,9,10] >>> b=[1,2,3 ...

  2. Linux 日志切割工具cronolog详解

    一.前言 二.cronolog 简介 三.cronolog 特点 四.cronolog 安装 五.cronolog 使用 六.cronolog 总结 注,操作系统 CentOS 6.4 x86_64, ...

  3. ceph-RGW Jewel版新概念

    一.概述 zone: 包含多个RGW实例的一个逻辑概念.zone不能跨集群,同一个zone的数据保存在同一组pool中: zonegroup:一个zonegroup如果包含一个或多个zone,如果包含 ...

  4. [Android实例] Android之断点续传下载

    在我们做开发的时候经常遇到的就是下载了,现在下载的方法有很多很多,那么怎么做到断点续传下载呢!很多人都头疼这个问题,如果我们没有很好的逻辑真不是很容易解决啊.我参考了一下前辈们的资料了整理了一个项目, ...

  5. Hadoop学习:Map/Reduce初探与小Demo实现

    原文地址:https://blog.csdn.net/liyong199012/article/details/25423221 一.    概念知识介绍 Hadoop MapReduce是一个用于处 ...

  6. How to get current timestamps in Java

    How to get current timestamps in Java Timestamp timestamp = new Timestamp(System.currentTimeMillis() ...

  7. Python 爬虫 解决escape问题

    爬取某个国外的网址,遇到的编码问题 ,在前段页面 返回的数据是 亞洲私人珍藏賣,令仝好分享他為此 所傾注的心血與熱愛。 爬虫源码是: url = 'http://www.bonhams.com/auc ...

  8. springboot 整合 Redis 方法二

    方法一请参考之前博文 spring boot 整合 redis 自己的版本  java8 + redis3.0 + springboot 2.0.0 1 spring boot已经支持集成 redis ...

  9. java中多个数字运算后值不对(失真)处理方法

    最近遇到一个bug ,在java里面计算两个数字相减,633011.20-31296.30 得到的结果居然是601714.8999999999,丢失精度了,原来这是Java浮点运算的一个bug. 解决 ...

  10. unity, Collider2D.attachedRigidbody

    boss根节点上挂RigidBody2D(且boss根节点以下任何子节点均不挂RigidBody2D),boss腿部骨骼节点挂collider2D,标签为"bossLeg",bos ...