题目:

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. Android Drawable系列(1):自定义背景以及注意事项

    0. Shape自身属性 android:shape=["rectangle" | "oval" | "line" | "ring ...

  2. VBA文件处理

    Option Explicit ' ▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽ ' Excel对象 ' △△△△△△△△△△△△△△△△△△ ' Open Public Function FileOpen_ ...

  3. Razor语法大全(转)

    Razor语法大全 因为最近在看mvc的时候在学习Razor的发现了这个不错的博文,故转之. 本文页面来源地址:http://www.cnblogs.com/dengxinglin/p/3352078 ...

  4. CStdioFile 写文件

    前言: 介绍如何使用 CStdioFile 类去写文件. 完整工程代码,点我下载(请注意工程里面需要包含的 <locale.h>头文件 ) 示例: /// My Add // 获取当前路径 ...

  5. 3D Touch ? 木有6s,也阔以玩!!!

    3D Touch 之 Peek & Pop 3D Touch 是iOS9之后专为 iPhone6s 机型加入的新特性,这一新技术移植于 Mac Book 上的 ForceTouch 更准确地说 ...

  6. javascript 第27节 jQuery选择器

    下面的html需要以下2个文件: 1.style.css div,span,p { width:140px; height:140px; margin:5px; background:#aaa; bo ...

  7. hibernate的dao操作不能提交到数据库问题的解决

    刚学的时候总是各种错误,解决方法也无厘头的很 将UserDAO里面的的save方法修改try { getSession().save(transientInstance); log.debug(&qu ...

  8. OpenJudge/Poj 1753 Flip Game

    1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...

  9. Redis和Memcache的区别分析 [转]

    1. Redis中,并不是所有的数据都一直存储在内存中的,这是和Memcached相比一个最大的区别. 2. Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构 ...

  10. 掌握 ActionResult

    我在上一篇博客不要停留在表面,MVC 3 我们要深入一些 说明了我们的掌握程度还是不够,还需要我们继续努力.但是有园友质疑说他们认为我说的只是书院派,并不实用,这令作为程序员的我很是生气.好吧,那咱们 ...