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 ...
随机推荐
- 深入理解java的抽象类和接口(转载)
原文链接:http://www.cnblogs.com/dolphin0520/p/3811437.html 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的 ...
- 转载--- SQL Server XML基础学习之<4>--XPath
T-SQL 支持用于查询 XML 数据类型的 XQuery 语言. XQuery 基于现有的 XPath 查询语言,并支持更好的迭代.更好的排序结果以及构造必需的 XML 的功能. 所以我们本章先 ...
- Java_Vector类的使用,以及Stack继承Vector,推出的栈的特性
测试用例: import java.util.Stack; /* * 简单的栈类测试: * Stack继承自Vector向量类: * 所以Stack的使用和Vector的使用类型的 * 而且是线程安全 ...
- 电脑中java环境的搭建
- CSS3的几个标签速记1
border-radius:CSS3圆角 语法:border-radius:25px; 椭圆边角:语法-border-radius:xx%;或者15px/100px; box-shadow ...
- 04_线程的创建和启动_使用Callable和Future的方式
[简述] 从java5开始,java提供了Callable接口,这个接口可以是Runnable接口的增强版, Callable接口提供了一个call()方法作为线程执行体,call()方法比run() ...
- 学习C++ Primer 的个人理解(十)
标准库没有给每个容器都定义成员函数来实现 查找,替换等操作.而是定义了一组泛型算法,他们可以用于不同类型的元素或多种容器类型. 迭代器令算法不依赖与容器 算法永远不会执行容器的操作 算法本身不会执行容 ...
- 九度OJ1198 a+b 【高精度整数】
题目地址:http://ac.jobdu.com/problem.php?pid=1198 题目描述: 实现一个加法器,使其能够输出a+b的值. 输入: 输入包括两个数a和b,其中a和b的位数不超过1 ...
- OpenJudge/Poj 1004 Financial Management
1.链接地址: http://poj.org/problem?id=1004 http://bailian.openjudge.cn/practice/1004 2.题目: 总时间限制: 1000ms ...
- Linux中的sed
sed [选项] [动作] 文件 选项: -n :静默模式.使用-n则只有经过sed处理的那一行. -e :允许多重编辑: -f :结果默认输出到终端,使用-f会将结果写在 ...