Skip to content

underscore-en/leetcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 

Repository files navigation

Non-trivial algorithms

Boyer–Moore Majority Vote Algorithm

Given a list of int, we can find the mode with O(1) space.

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int counter = 0;
        int candidate;

        for (const auto& n: nums) {
            if (counter == 0) {
                candidate = n;
            }
            counter += (n == candidate) ? 1 : -1;
        }
        return candidate;
    }
};

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published