Lintcode: Majority Number II 解题报告
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.
There is only one majority number in the array
For [1, 2, 1, 2, 1, 3, 3] return 1
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 解题报告的更多相关文章
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- 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 ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
随机推荐
- MM bound 与 Jensen's inequality
MM bound 与 Jensen's inequality 简森不等式 在使用最大似然估计方法求解模型最优解的时候,如果使用梯度下降(GD or SGD)或者梯度上升(GA or SGA),可能收敛 ...
- iOS开发调试Reveal使用
推荐通过Xcode中加断点的方式集成Reveal(小缺陷,当你禁用断点时或者不用Xcode而用Appcode开发时,这个方式是不管用). 打开您的iOS工程,选择 View → Navigators ...
- C#项目中引入app.manifest管理员权限运行
原文地址:https://blog.csdn.net/qq395537505/article/details/51010962 参考UAC:User Account Control 参考IE保护模式: ...
- java 高性能读模式(译)
原文地址:http://tutorials.jenkov.com/java-performance/read-patterns.html 好久没翻译了,逛知乎,无意间发现的一个链接,写的太好了,而且内 ...
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated
启动mysql时,报如下警告信息: [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explic ...
- FreeSWITCH增加iLBC编码
1. 安装ilbc库从第三方库里下载指定版本 git clone https://freeswitch.org/stash/scm/sd/libilbc.git ./bootstrap.sh ./co ...
- Sublime text —— 自定义主题Soda
编辑器的主题有两种,一种是语法高亮颜色主题,一种是编辑器自身显示主题,如果要自定义编辑器样式,个人推荐soda. Ctrl+Shift+p 输入install,接着输入 soda,选择 Theme ...
- golang包time用法详解
在我们编程过程中,经常会用到与时间相关的各种务需求,下面来介绍 golang 中有关时间的一些基本用法,我们从 time 的几种 type 来开始介绍. 时间可分为时间点与时间段,golang 也不例 ...
- SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport
场景及需求: 项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串. 例如: [ { "id": 1, ...
- svn提交遇到冲突解决方法
冲突:如果提交时候发现冲突了先不急着提交,否则会产生冲突文件. 解决步骤: 1.将本地文件先复制一份 2.svn revert(恢复到没修改前版本) -> svn update(更新当前最新版本 ...