169. Majority Element
题目:
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
链接: http://leetcode.com/problems/majority-element/
题解:
找Major Element, more than [n / 2] times。可以使用Boyer-Moore's Majority Voting Algorithms - 设定一个candidate,一个count。在count = 0时设置candidate为当前nums[i],假如candidate == nums[i],count++,否则count--。 可能会有的follow up是,找more than [n / 3] 和 [n / 4] times的major elements, 这时我们要根据[n / theta]中theta的大小来设置一个HashMap<Integer,Integer>来记录Integer以及count。当integer不在map中时,我们把put(integer, 1),否则put(integer, count + 1)。当map.size() > [1/theta]时,遍历map,每个key的count--,当count == 0时,delete这个key。 最后返回map中count > 1/theta的值。 这样的花时间复杂度是O(n),空间复杂度是O(1)。
Time Complexity - O(n), Space Complexity - O(1)。最后其实应该再过一遍数组来检查这个元素是否是majority element
public class Solution {
public int majorityElement(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int candidate = 0, count = 0;
for(int i = 0; i < nums.length; i++) {
if(count == 0)
candidate = nums[i];
if(nums[i] == candidate)
count++;
else
count--;
}
return candidate;
}
}
又思考了一下general的case,假如给定一个数组,要求majority elements,这些elements出现 > 数组的theta (比如theta = 0.3, 0.4,0.5)。方法应该是如下: Time Complexity - O(n), Space Complexity - O(1 / theta)。
1) 建立一个k个数的map, 比如more than [1/2]则 k = 1, more than [1/3]则 k = 2, more than [1/4]则 k = 3。
2) 当map.size() > k时,对map中每个元素进行count--,移除 = 0元素
3) 对map中剩余元素进行count++
4)最后输出map中count > theta * n的元素。
二刷:
Boyer-Moore选择问题。可以使用一个count一个candidate很容易的找到比n/2大的元素。要注意的是题目给出这样元素存在在数组里, 假如没有这个条件,我们在最后还要再判断一下这个candidate是否count > n / 2.
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int majorityElement(int[] nums) {
if (nums == null | nums.length == 0) {
return -1;
}
int candidate = 0, count = 0;
for (int i = 0; i < nums.length; i++) {
if (count == 0) {
candidate = nums[i];
count++;
} else {
count = nums[i] == candidate ? count + 1 : count - 1;
}
}
/*
count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == candidate) {
count++;
}
if (count > nums.length() / 2) {
break;
}
}
return count > nums.length() / 2 ? candidate : -1;
*/
return candidate;
}
}
三刷:
Boyer-Moore选择
Java:
public class Solution {
public int majorityElement(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int candidate = 0, count = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
count++;
} else if (num == candidate) {
count++;
} else {
count--;
}
}
for (int num : nums) {
if (candidate == num) {
count++;
}
}
return (count >= nums.length / 2) ? candidate : 0;
}
}
Reference:
http://www.cs.yale.edu/homes/el327/datamining2011aFiles/ASimpleAlgorithmForFindingFrequentElementsInStreamsAndBags.pdf
https://leetcode.com/discuss/43248/boyer-moore-majority-vote-algorithm-and-my-elaboration
http://stackoverflow.com/questions/24691048/find-all-elements-that-appear-more-than-n-4-times-in-linear-time
https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
169. Majority Element的更多相关文章
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
- 169. Majority Element - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- (Array)169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- Oracle学习第一天---安装和基础入门
国庆七天假,决定静下心来入门Oracle数据库. 环境:Ocacle 11g 软件安装包和安装图解,大家可以在我的百度网盘上下载:链接:http://pan.baidu.com/s/1ntjDEnZ ...
- ### 学习《C++ Primer》- 6
Part 6: 拷贝控制(第13章) // @author: gr // @date: 2015-01-08 // @email: forgerui@gmail.com 一.拷贝.赋值与销毁 拷贝构造 ...
- C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)
学习书籍: <C#本质论> 1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是 ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- tail报错
在block和index都没有满的情况下,有如下报错: tail -f messages tail:cannot watch 'messages' : No space left on device ...
- iis7.5 aspx,ashx的mime类型
映射aspx: 打开IIS管理器,找到“处理程序映射”,在列表右击选择“添加脚本映射”即可.eg:*.aspx,将该类型的页面的处理程序映射为“%windir%\Microsoft.NET\Frame ...
- python引入导入自定义模块和外部文件
参考:http://blog.csdn.net/devil_2009/article/details/15816237 项目中想使用以前的代码,或者什么样的需求致使你需要导入外部的包 如果是web 下 ...
- ORA-24010 SMG-3000
参考:http://t.askmaclean.com/thread-528-1-1.html 运行脚本的时候一定要清楚脚本做了什么操作,注意看清脚本的报错. conn / as sysdba@?/rd ...
- 2014年度辛星css教程夏季版第七节
本小节我们研究浮动的问题,浮动使得布局更加灵活,虽然我们前面也有关于布局的讲解,但是它们提供的内容还是太有限了,要想获得更强大的布局,还必须使用浮动才能完成更灵活的布局. ***********浮动* ...
- Nhibernate配置和访问数据问题
今天开始用Nhibernate做为自己的ORM,但是做的过程中确实遇到了好多问题,现在将问题收集起来以防日后出现相同的问题, 总结下: 这就是我的整个项目,现在配置下hibernate.cfg.xml ...