Lintcode: 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
三三抵销法,但是也有需要注意的地方:
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的更多相关文章
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- 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: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- 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 ...
- Lintcode: Majority Number 解题报告
Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- [LintCode] Majority Number 求大多数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- LintCode: Single Number II
一篇解析比较详细的文章:http://www.acmerblog.com/leetcode-single-number-ii-5394.html C++ 解法(1) 求出每个比特位的数目,然后%3,如 ...
随机推荐
- SQL查询(二)
常用查询技巧 1.获取数据的前3(n)行 ; 2.SQL语句中if语句 在SQL语句中没有直接的if语句,但是有两个函数:DECODE和CASE,他们能够实现if语句的功能 2.1)decode -- ...
- laravel 部分路由取消csrf
// app/Http/Middleware/VerifyCsrfToken protected $except = [ 'webhook/*' ];
- Git版本控制
官方文档:http://git-scm.com/book/en/v2 github :https://guides.github.com/activities/hello-world/ How to ...
- ftp 229
在sels10机器上登入ftp输入用户名和密码之后再ls发现出现如下问题Entering Extended Passive Mode ftp> ls229 Entering Extended P ...
- 内存分配、C++变量的生命周期和作用域
1.内存分配 程序的内存分配有以下几个区域:堆区.栈区.全局区.程序代码区,另外还有文字常量区. 栈区 ——存放局部变量,即由auto修饰的变量,一般auto省略.由编译器自动分配释放.局部变量定义在 ...
- ASP.NET中身份验证
ASP.NET中身份验证有三种方式:Windows.Forms和Passport. 1.Windows验证,基于窗体验证,需要每个页面写上验证身份代码,相对灵活,但操作过于复杂: 2.Passport ...
- Notepad++ install vi plugin
下载Notepad++,想安装vi插件. 使用Notepad++自带的插件管理器下载visimulator失败. 所以直接下载插件visimulator.dll,再导入. 下载地址: https:// ...
- Java 实现网站当前在线用户统计
1. import java.util.HashSet; import javax.servlet.ServletContext; import javax.servlet.http.HttpSess ...
- TermServDevices报错导致服务器死机(远程服务使用者必读)
事件类型: 错误 事件来源: TermServDevices 事件 ID: 1111 描述:打印机 !!192.168.99.6!HP LaserJet 3050 Series PCL 5e 所需的驱 ...
- 另一个SqlParameterCollection中已包含SqlParameter
一般情况下,我们定义的一个SqlParameter参数数组,如: SqlParameter[] parms = { new SqlParamete ...