[ISSUE] Majority Vote

源引自己的github.io

http://awarrior.github.io/majority-vote/

Describe

Give you a log of searching that contains N words, how to find out one key word which appears over N/2 times (definitely exists), just using O(n) time complexity?

Analyze

There is a very important pre-condition in this issue: the target word which we want to find definitely exists.

Let’s see several examples first:

(A B) C A A B
(A B) C A A B A
It doesn’t matter if we leave A&B out, A is still the result.

(C B) A A A B A
It doesn’t matter if we leave C&B out, A is still the result.

(A A) C B A B A
We have to record the apperance times of A for further judgement.

Here we consider two word each time. A majority word is still a major one if we minus two words which are different from each other. Otherwise, if the two words are the same, we also need to save the information of this word.

Actually, we can consider this words sequence as conbination of several word pairs (two words).

Resolve

We can set two variables to use, one for saving current word (p) and another for counting (c). Initialize c=0 and p points to the front of the first word.

Make a rule while we iterate the words sequence like this:

  1. If c=0, p saves no word;
  2. Else if the next word equals the current one, c+=1; else c-=1.

You can see the whole process in this website for details:

http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html

PAPER TITLE: MJRTY - A Fast Majority Vote Algorithm

Code

// use number expression for example
int n[10] = { 2, 1, 2, 3, 4, 2, 2, 1, 2, 2 };
int c = 0, p;
for (int i : n) {
    if (c == 0) {
        p = i;
        ++c;
    } else {
        if (i == p)
            ++c;
        else
            --c;
    }
}
std::cout << c << std::endl << p << std::endl;
时间: 2024-10-11 21:21:35

[ISSUE] Majority Vote的相关文章

多数投票算法(Majority Vote Algorithm)

在面试题中经常会出现这样一个题目,给一个数组,其中含有N个非负元素,让你求出数组中出现次数超过一半的数字. 看到这个问题我们首先想到的可能是暴力的解法,那就是将数组排个序,输出中间的元素就行了,因为如果出现次数超过一半的话排完序后中间的那个元素肯定是我们需要求的值. 这样做的话排序的时间复杂度一般来说是O(NlogN),那么有没有时间复杂度为n的算法呢? 答案当然是有的,有这样的一个算法,Majority Vote Algorithm,他是这样的做的:设置一个计数器count和保存最多元素的变量

A Linear Time Majority Vote Algorithm

介绍一种算法,它可以在线性时间和常数空间内,在一个数组内找出出现次数超过一半的某个数字. 要解决这个问题并不难,可以使用排序或哈希,但是这两种算法都不能同时满足时间或空间的要求. 然而,该算法(A Linear Time Majority Vote Algorithm )却可以在同时满足这两个条件的情况下完美地解决问题. 现在将该算法简单描述如下: 对于数组中出现的某个数字设为待定数字,如果它出现则将其出现次数加一,如果没有出现则减一,如果减至零则将当前数字更换为新的待定数字.这样线性遍历之后可

leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

题目: 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. 题解:运用多数投票算法的思路来解:从头到尾遍历数

Task 待学习内容【Moore majority vote algorithm(摩尔投票算法)】

https://www.cnblogs.com/zhonghuasong/p/6536665.html 参见:https://my.oschina.net/u/2277632/blog/2873164 原文地址:https://www.cnblogs.com/leodaxin/p/11355590.html

leetcode 229. Majority Element II(多数投票算法)

就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int> majorityElement(vector<int>& nums) { int cnt1=0,cnt2=0,ans1=0,ans2=1; for(auto n:nums){ if(n==ans1){ cnt1++; } else if(n==ans2){ cnt2++; } el

Majority Element in an Array

Problem Statement Given a large array of non-negative integer numbers, write a function which determines whether or not there is a number that appears in the array more times than all other numbers combined. If such element exists, function should re

Lintcode: Majority Number 解题报告

Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example For [1, 1, 1, 1, 2, 2, 2], return 1 Challenge O(

leetcode 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. 思路: Find k different element

Majority Element &amp;&amp; Majority Element II

Majority Element https://leetcode.com/problems/majority-element/ Difficulty: Easy 查找数组的多数元素(majority element) 多数元素为数组中出现次数多于?n/2?的元素.假设数组非空且多数元素一定存在 LeetCode的解答中给出了七种思路 第一种是Brute force solution,时间复杂度为O(n2),顾名思义,遍历数组,依次判断每个元素是否为多数元素 第二种是Hash table,时间复