当前位置:网站首页>Moorish voting
Moorish voting
2022-06-22 02:48:00 【SS_ zico】
Moore voting (Boyer–Moore majority vote algorithm) From the paper
《MJRTYA Fast Ma jority
Vote Algorithm》
The problem solved by the algorithm is how to choose any number of candidates ( Ballot disorder ), Choose the one who gets the most votes . The common algorithm is to scan the votes , Count the votes counted for each candidate . When the number of candidates is fixed , The time complexity of this common algorithm is :O ( n ) , When the number of candidates varies , Counting votes may take a long time , May need to run O ( n^2 )
) Time for . When the votes are in order , It's easy to make up O ( n ) The program , First find the median , Then check whether the median number exceeds half of the vote . This paper aims at the situation of disorder and uncertain candidates , Moore voting algorithm is proposed . The maximum number of comparisons of the algorithm is votes ( Write it down as n) Twice as many , Can be in O ( n ) Choose the one who gets the most votes in time , The space cost is O ( 1 ) .
Visualize :
The core is the cost of competition .
Play a game of princes fighting for supremacy , Suppose your population is more than half of the total population , And it can ensure that every population can die one-on-one when they go out to fight . In the end, the country where people survive is victory .
Scuffle time , Maybe everyone will unite against you ( Every time you choose as a counter, the number is mode ), Or other countries will attack each other ( Other numbers will be selected as the number of counters ), But as long as you don't fight inside , In the end, you're sure to win .
In the end, the rest must be our own people .
Algorithm application
One of the great applications of Moore voting is to find the mode .
Related topics are :
1
Power button 169. Most elements
class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
int c = 1;
int num = nums[0];
for(int i = 1;i < n;i++)
{
if(num == nums[i])
c++;
else{
c--;
if(!c)
num = nums.at(i+1);
}
}
return num;
}
};
2
Power button 229. Find mode II
It can be seen as such a situation : A class should elect a deputy monitor , at most 2 position . One vote for each , To become a vice squad leader, you must get more than one third of the total votes .
Because there may be two . So the candidate cand And counting cnt To the corresponding array form cands And cnts, The length is 2.
In the first stage, it is necessary to offset in pairs ,cands[0] And cands[1] Of the votes do not cancel each other out , That is, if the delegate votes for cands[0], be cands[1] Corresponding cnts[1] The value of does not change .
To vote for cands[1] The same is true . This translates into the original case of the Moorish voting method .
The second stage counts , In addition to determining whether two candidates have more than a third of the votes , It is also necessary to determine whether the two candidates are the same .
/* The time complexity is :O(n) The space complexity is :O(1) */
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int len = nums.size();
vector<int>res, cands, cnts;
if(len == 0){
// There is no element , Returns an empty array directly
return res;
}
cands.assign(2, nums[0]);
cnts.assign(2, 0);
// The first 1 Stage Set off in pairs
for(auto num: nums){
bool flag = false;
for(int i = 0; i < cands.size(); ++i){
if(num == cands[i]){
++cnts[i];
flag = true;
break;
}
}
if(!flag){
bool flag2 = false;
for(int j = 0; j < cands.size(); ++j){
if(cnts[j] == 0){
flag2 = true;
cands[j] = num;
cnts[j]++;
}
}
if(!flag2){
for(int j = 0; j < cnts.size(); ++j){
--cnts[j];
}
}
}
}
// The first 2 Stage Count The number should be more than one third
cnts[0] = cnts[1] = 0;
if(cands[0] == cands[1])
cands.pop_back();
for(auto num:nums){
for(int i = 0; i < cands.size(); ++i){
if(cands[i] == num){
++cnts[i];
break;
}
}
}
for(int i = 0; i < cands.size(); ++i){
if(cnts[i] > len / 3){
res.push_back(cands[i]);
}
}
return res;
}
};
application 3
At most m A representative , The number of votes per ballot is greater than n / (m + 1)
Just put the question 2 Determine whether the final candidate is the same code for modification .
边栏推荐
- 最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
- Markdown advanced syntax, marktext compatible
- Object detection -- how to use labelimg annotation tool
- fatal error: png++/png.hpp: 没有那个文件或目录
- Official release of ideal L9: retail price of 459800 yuan will be delivered before the end of August
- FPGA-Xilinx 7系列FPGA DDR3硬件设计规则
- Implementation differences between import and require in browser and node environments
- Graphacademy course explanation: Fundamentals of neo4j graph data science
- 关于PMP考试,你想知道的知识都在这里了
- Two dot vertical progress styles
猜你喜欢
![[go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network](/img/91/324d04dee0a191725a0b8751375005.jpg)
[go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network

EMC整改小技巧

Asemi Schottky diode 1N5819 parameters, 1N5819 replacement, 1N5819 source

JS special effects in the construction of animated web pages

最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA

FPGA-Xilinx 7系列FPGA DDR3硬件设计规则

Architecture and practice of vivo container cluster monitoring system
![[3. binary integer and floating point number]](/img/82/6c3ef250b90d875cddaebc5bd4a4b8.png)
[3. binary integer and floating point number]

C3-qt realize Gobang games (I) November 7, 2021

【7. 高精度除法】
随机推荐
How to select the appropriate version of neo4j (version 2022)
2022钎焊考试模拟100题及答案
Day12QFile2021-09-27
理想L9正式发布:8月底前开始交付 零售价45.98万元
discuz! Bug in the VIP plug-in of the forum repair station help network: when the VIP member expires and the permanent member is re opened, the user group does not switch to the permanent member group
[5. high precision subtraction]
UnionPay payment return merchant nignx post request 405
使用 OKR 進行 HR 數字化轉型
Day21qt mouse event 2021-11-01
[1. quick sort]
Introduction to Apache ActiveMQ Artemis
xpm_memory_tdpram原语的完整使用实例
GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
With the acceleration of industry wide digital transformation, what kind of storage will be more popular?
FPGA Xilinx 7 Series FPGA DDR3 hardware design rules
PMP pre exam guide on June 25, you need to do these well
EMC整改小技巧
Ioerror: no translation files found for default language zh cn Solutions for
Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd
【1. 快速排序】