LeetCode OJ 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times. The algorithm should run in linear time and in O(1) space.
Hint:
- How many majority elements could it possibly have?
- Do you have a better hint? Suggest it!
【题目分析】
在Majority Element中我们使用了一个巧妙的算法就是Moore's alogoirthm,在上一个题目中我们寻找的是the elements that appear more than ⌊ n/2 ⌋
times。
假设⌊ n/2 ⌋
= k,那么n最大值为2k+1,Majority Element至少为k+1个,因此Majority Element最多只能有一个。我们设置两个变量 ME 和count,ME用来存储当前的Majority Element,count对当前ME进行计数,计数规则如下。遍历数组,如果count = 0,则把当前元素赋给ME;否则的话如果当前元素=ME,则count++,否则count--;这个过程结束和,最后的EM肯定是我们Majority Element。
那么对于当前题目,设⌊ n/3 ⌋ = k,那么n最大为3k+2。如果有这样的元素它出现的个数大于k,那么这样的元素最多有两个(3k+2-2k-2 = k)。我们是否还可以用Moore's alogoirthm来解决我们的问题呢?
因为最多只能有两个Majority Element,我们设置如下几个变量:EM1,EM2,count1,count2,用来表示两个Majority Element和他们的计数。我们讨论一下下面几种情况:
1. 不存在Majority Element。那么我们最后会得到一个结果,但是这个结果可能是不正确的,需要重新遍历一次数组来对找到的元素进行计数;
2. 存在两个Majority Element。我们知道 count(EM1) >= k+1, count(EM2) >= k+1,剩余的数字 count(remain) <= k。遍历数组的过程中,如果当前元素和EM1,EM2都不相同,那么count1--,count2--,最后的结果肯定是Majority Element,因为他们的数目比其他元素多,count数不会被减到0;
3. 存在一个Majority Element。此时count(EM1) >= k+1,那么在算法执行的过程中EM1是否会被减到0呢?比如k+1个EM1出现在数组的最前面,在遍历到其他数字时,由于count(remian)<= 2k+2,因此我们担心这样的过程会导致不能找到正确的Majority Element,但事实是我们会得到正确的结果。假设最坏的情况:剩下的k+2个数字是互不相同的,如下:
[1,1,1,1,2,3,4,5]
这个数组中有8个元素,Majority Element是1,剩下的元素互不相同。在遍历前四个元素的时候count1加到4,当到达第五个元素时候,该元素不等于EM1,此时count2 = 0,因此我们会把当前元素赋值给EM2,此时count1并不发生变化。遍历到第六个元素时,当前元素与EM1和EM2都不相同。此时count1和count2才会减1。继续这个过程直到遍历完成所有的元素,我们可以看到在这个过程中,剩下的元素有一半会被赋值给EM2,而另一半元素才会使得count1--。因此count1最多减(2k+1)/2 = k次,所以如果只有一个Majority Element的话,它肯定会出现在结果中。上面只是举了最坏的情况,对于任意一种排列方式,和任意的情况这个结论都是成立的。
【java代码】
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> list = new ArrayList<Integer>();
if(nums == null || nums.length == 0) return list; int num1 = nums[0], num2 = 0;
int count1 = 0, count2 = 0; for(int i = 0; i < nums.length; i++){
if(num1 == nums[i]) count1++;
else if(num2 == nums[i]) count2++;
else if(count1 == 0){
num1 = nums[i];
count1++;
}
else if(count2 == 0){
num2 = nums[i];
count2++;
}
else{
count1--;
count2--;
}
} count1 = 0; count2 = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == num1) count1++;
else if(nums[i] == num2) count2++;
}
if(count1 > nums.length/3) list.add(num1);
if(count2 > nums.length/3) list.add(num2); return list;
}
}
LeetCode OJ 229. Majority Element II的更多相关文章
- 【LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【刷题-LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- LeetCode OJ:Majority Element II(主元素II)
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- [LeetCode] 229. Majority Element II 多数元素 II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- Leetcode # 169, 229 Majority Element I and II
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 229. Majority Element II My Submissions Question
Total Accepted: 23103 Total Submissions: 91679 Difficulty: Medium Given an integer array of size n, ...
随机推荐
- [MFC美化] SkinSharp使用详解2-SkinH.h函数介绍
SkinSharp功能强大,该皮肤库支持完全多种颜色改变等. 下面是静态链接库时的SkinH.h头文件: /*在Stdafx.h文件中加入如下语句 #include "SkinH.h&quo ...
- HDU 4520 小Q系列故事——最佳裁判
Time Limit : 500/200ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Problem Description 过 ...
- Playmaker 基础使用与案例操作
首先是把下载好的插件导入Unity工程中. ▼导入完成后第一个动作就是检查下拉菜单里面是否已经增加了Playmaker的功能,如果在安装后没看到Playmaker的菜单,一般情况下直接点击菜单上的空白 ...
- pgmpy包的安装,以及conda的安装
pgmpy包的安装,以及conda的安装 pgmpy的官方网站是这样说明安装过程的: 链接:Installation 共有三个步骤,第一步是安装依赖包 首先它讲述了在linux下使用pip安装的方法: ...
- openvpn环境搭建
以下为服务端.客户端下载安装包,在CentOS 6.5部署,也可以找到相关下载源 openvpn-2.2.2.tar.gz,openvpn-2.1.3-install.rar,(https://git ...
- 连续多个git提交发生了冲突时
git checkout -b test 创建并切换到分支test git clone git branch master git merge test 合并test到master (git merg ...
- SQL Server 2008 定时作业的制定
--SQL Server 2008 定时作业的制定 --1.打开[SQL Server Management Studio],在[对象资源管理器]列表中选择[SQL Server 代理]: --2.鼠 ...
- Dubbo Zookeeper
发布 Service:每个<dubbo:service/>在spring内部都会生成一个ServiceBean实例,ServiceBean的实例化过程中调用export方法来暴露服务 co ...
- 使用Session防止表单重复提交
本文出处:http://www.cnblogs.com/xdp-gacl/p/3859416.html 在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以 ...
- proc中tran的一般处理
存储过程事物的处理 一般情况在proc中判断的处理我们都采用 ) begin select '错误' return end 而在tran中我们最好用抛异常的方式 begin try begin SET ...