题目:

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的更多相关文章

  1. 169. Majority Element(C++)

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  2. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. 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 ...

  4. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  5. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  6. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

  7. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  8. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. (Array)169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. LeetCode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. mysqlimport 导入文件到数据库命令

    mysqlimport -h 172.16.145.125 -u ocetl -pocetl test  --fields-terminated-by='|' '/home/ocetl/tmp_use ...

  2. CS0016: 未能写入输出文件“c:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\data\34aae060\b7daa87d\App_Web_addadvice.aspx.cdcab7d2.ekhlcbjd.dll”--“目录名无效。 ”

    产生原因: 应用程序运行时产生的临时文件需要存放到c:"windows"temp 文件夹下 而运行基于microsoft .net framework 框架下的应用程序 需要对te ...

  3. Markdown 生成目录

    <link rel="stylesheet" href="http://yandex.st/highlightjs/6.2/styles/googlecode.mi ...

  4. C--指针函数,static

    (*p)是固定写法,代表指针的变量P将来是指向函数 void (*p)(); p=test;//指针变量P指向了test函数 函数名test代表函数地址 //同等调用test()函数 (*p)(); ...

  5. [Guava官方文档翻译] 2.使用和避免使用null (Using And Avoiding Null Explained)

    本文地址:http://www.cnblogs.com/hamhog/p/3536647.html "null很恶心." -Doug Lea "这是一个令我追悔莫及的错误 ...

  6. OpenJudge/Poj 2027 No Brainer

    1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...

  7. Linux中的安装神器--yum源安装

    linux配置yum源 一.修改yum的配置文件     /etc/yum.repos.d/xxx.repo          1.进入yum配置文件目录        # cd /etc/yum.r ...

  8. centos6.3安装openvpn客户端

    centos6.3安装openvpn客户端 Centos 默认是没有提供Openvpn的,而且在yum 的源里面也没有openvpn ,如果想使用yum安装的话要首先安装EPEL这个东西.www.2c ...

  9. HDOJ(1005) Number Sequence

    这道题,咋一看很像Fibonacci数列,使用递归或者改进的动态规划来解决.但是仔细一看,(1 <= n <= 100,000,000),使用这些方法,要么超时,要么肯定内存不够用,因此必 ...

  10. javascript基础DOM操作

    DOM 中的三个字母,D(文档)可以理解为整个 Web 加载的网页文档:O(对象)可以理解为类似 window 对象之类的东西,可以调用属性和方法,这里我们说的是 document对象:M(模型)可以 ...