2020年 Moore majority vote algorithm 摩尔投票法知多少
第一眼看到这个题目,想到的是使用Map来统计出现频次,然后遍历找出频次大于n/2
的元素。
class Solution {public int majorityElement(int[] nums) {Map<Integer, Integer> map = new HashMap<>();for(Integer item: nums){if(null != map.get(item)){map.put(item, map.get(item) + 1);}elsemap.put(item,1);}for(Integer freq: map.keySet()){if(map.get(freq) > (nums.length / 2))return freq;}return -1;}
}
但是显而易见的是:没有用上n/2
这个设定。
其实这个看似简单的问题有一个特殊的解法,就是我们要说的***Boyer–Moore majority vote algorithm*** :摩尔投票法
知乎上有老哥给出了非常形象的解释:
核心是对拼消耗:类似我们玩的即时战略游戏:魔兽争霸,三国群英传等。假设地图上有一家(称作红色军)拥有所有军队中一半以上的小兵,在直接对拼下不虚任何对手(不同队伍小兵1v1地同归于尽),其他队伍像蓝色、绿色、紫色等,有可能会互相消耗,但是最后留在地图上的一定是同一队人数最多的红色。
如何理解摩尔投票算法? - 知乎用户的回答 - 知乎
https://www.zhihu.com/question/49973163/answer/617122734
具体实现:
class Solution{public int majorityElement(int[] nums){int res = 0;int count = 0;for (int num : nums) {if (count == 0) {res = num;}count += (num == res) ? 1 : -1;}return res;}
有兴趣的话还可以再看一下下面这个
wiki上的伪代码:
The algorithm maintains in its local variables a sequence element and a counter, with the counter initially zero.
It then processes the elements of the sequence, one at a time. When processing an element x, if the counter is zero, the algorithm stores x as its remembered sequence element and sets the counter to one.
Otherwise, it compares x to the stored element and either increments the counter (if they are equal) or decrements the counter (otherwise).
At the end of this process, if the sequence has a majority, it will be the element stored by the algorithm. This can be expressed in pseudocode as the following steps:Initialize an element m and a counter i with i = 0
For each element x of the input sequence:
If i = 0, then assign m = x and i = 1
else if m = x, then assign i = i + 1
else assign i = i − 1
Return m
Even when the input sequence has no majority, the algorithm will report one of the sequence elements as its result.
However, it is possible to perform a second pass over the same input sequence in order to count the number of times the reported element occurs and determine whether it is actually a majority. This second pass is needed, as it is not possible for a sublinear-space algorithm to determine whether there exists a majority element in a single pass through the input.
— 引用自wikipedia
2020年 Moore majority vote algorithm 摩尔投票法知多少相关推荐
- Boyer–Moore majority vote algorithm(摩尔投票算法)
Boyer–Moore majority vote algorithm 摩尔投票算法 Leetcode15: https://leetcode.com/problems/majority-elemen ...
- 摩尔投票法(Boyer–Moore majority vote algorithm)
参考资料 论文MJRTY A Fast Majority Vote Algorithm 算法演示网站 维基百科 算法解读 概述 摩尔投票法(Boyer–Moore majority vote algo ...
- 小小算法,可笑可笑——摩尔投票法(集万家之长)
摩尔投票法 不多说,先上题目. 问题描述:leetcode 229题 给定一个大小为 n 的整数数组,找出其中的所有的出现超过 ⌊ n/3 ⌋ 次的元素. 这还不简单!直接暴力计数,上map,easy ...
- java 投票算法_Boyer and Moore Fast majority vote algorithm(快速选举算法)
问题来来自于leetcode上的一道题目,https://leetcode.com/problems/majority-element/,大意是是找出一个数组中,出现次数超过一个半的数字,要求是O(n ...
- 多数投票算法 Majority Vote Algorithm
题目 Given an integer array of size n, find all elements that appear more than ? n/3 ? times. The alg ...
- 动态规划和摩尔投票法
动态规划 维基百科对动态规划(Dynamic programming,简称DP)的定义是一种在数学.管理科学.计算机科学.经济学和生物信息学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问 ...
- 摩尔投票法(力扣- -229. 求众数 II)
摩尔投票法(力扣- -229. 求众数 II) 文章目录 摩尔投票法(力扣- -229. 求众数 II) 一.题目描述 二.分析 摩尔投票法 总结 三.代码 一.题目描述 二.分析 这道题如果用O(N ...
- [剑指offer][JAVA]面试题第[39]题[数组中出现次数超过一半的数字][HashMap][摩尔投票法]
[问题描述][简单] 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.你可以假设数组是非空的,并且给定的数组总是存在多数元素.示例 1:输入: [1, 2, 3, 2, 2, 2, 5, ...
- 【LeetCode笔记】169. 多数元素(Java、摩尔投票法、哈希表)
文章目录 题目描述 思路 & 代码 思路一:哈希表 思路二: 摩尔投票法 题目描述 好家伙,这是今天最有意思的题目了 思路 & 代码 思路一:哈希表 先说缺点:空间复杂度O(n) 一次 ...
最新文章
- Python黑客编程基础3网络数据监听和过滤
- rm -rf ~/.bashrc 的惨痛教训
- How is a Batch request handled in the backend
- oracle 临时文件 大文件,Oracle中临时文件File#和Db_files关系
- 由浅入深解释JS执行机制 EventLoop
- 每月拿几百元来买基金,有意义吗?
- 查看是否存在DDOS*** netstat
- 《CSS揭秘》:菱形图片
- 判断数据是增量分区全量分区
- cocos2d-x-3.x 配置(1)win环境搭建
- IBM X3650 M4 主板故障
- 弹簧设计计算软件简介
- opencv Rect函数裁剪图片
- jboss-5.1.0_JBoss企业SOA平台5.0和Developer Studio 3.0
- pdf转换成word后有文字叠加_PDF转换成Word后进行处理
- hardfault常见原因_STM32 出现 hardfault_handler 处理方法
- adb 不可以网络连接问题
- 洪水填充算法_洪水填充算法分析
- 百度天气预报接口使用详细
- linux 私有云存储,私有云存储搭建(owncloud)
热门文章
- 迁移废弃的Kotlin Android Extensions插件
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #12 使用Memory Cgroup限制内存使用量...
- python怎么导入pandas库_Python之2-Pandas数据导入导出
- 妈耶,爆炸了!Oracle发布全栈虚拟机GraalVM,全力支持 Python!
- 计算机识别不了外接硬盘分区,U盘或移动硬盘能识别但不能分配盘符怎么办解决教程...
- 软件需求规格说明书(宿舍小助手)
- 如何提高Python的运行速度?(干货)
- 【强化学习】开源环境集(魂斗罗、星际争霸等都有!)
- windows下yacc和lex开发环境配置(Parser Generator篇)
- android 代码设置tint,Android Tint 使用