Leetcode 229 Majority Element II

Source: Internet
Author: User

The basic algorithm used in this problem is Boyer–moore majority vote algorithm

There are sample code in the Wiki

 1 Import java.util.*; 2 public class Majorityvote {3 public int majorityelement (int[] num) {4 int n = num.length; 5 int CA Ndidate = num[0], counter = 0;  6 for (int i:num) {7 if (counter = = 0) {8 candidate = i; 9 counter = 1;10} else {one (i = = candidate) {counter++;13}         else {counter--;15}16}17}18 counter = 0;20 for (int i:num) {if (i = = candidate) counter++;22}23 if (counter < (n + 1)/2) RET urn-1;24 return candidate;25}27 public static void Main (string[] args) {Majorityvote s = n EW Majorityvote (); System.out.format ("%d\n", S.majorityelement (new int[] {1, 2, 3})); T ("%d\n", S.majorityelement (new int[] {2, 2, 3}); 31}32}

The basic idea is this: there is at most one element in the array that has more than N/2, so there is only one candidate element during the traversal

We assume that there is an element that satisfies this requirement: if he is evenly distributed, at least once every interval, and not enough, at least one more time at some point.

Now think about the uneven situation: if the element is spaced out for a long time, there is at least a dense area in front or behind the long interval.

Vector<int> majorityelement (vector<int>& nums) {if (Nums.empty ()) return vector<int> ();    if (nums.size () = = 1) {return vector<int> ({nums[0]});    } vector<pair<int, int>> candidates;        for (auto num:nums) {if (Candidates.size () < 1) {candidates.emplace_back (num, 2); } else if (Candidates.size () < 2) {if (num! = Candidates[0].first) Candidates.emplace_ba            CK (num, 2);        else candidates[0].second++;                } else{if (num = = Candidates[0].first) {candidates[0].second++;            candidates[1].second--;                } else if (num = = Candidates[1].first) {candidates[1].second++;            candidates[0].second--;                } else{candidates[0].second--;                candidates[1].second--; int ind = Candidates[0].second < candidates[1]. Second?                0:1;                    if (candidates[ind].second <= 0) {candidates[ind].first = num;                Candidates[ind].second = 2;    }}}} for (auto& candidate:candidates) {candidate.second = 0;                } for (auto Num:nums) {for (auto& candidate:candidates) {if (num = = Candidate.first)        candidate.second++;    }} vector<int> result; for (auto candidate:candidates) {if (Candidate.second > Nums.size ()/3) Result.push_back (Candidate.fir    ST); } return result;

  

Leetcode 229 Majority Element II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.