"Original" Leetcodeoj---Majority element problem solving report (a popular search for elements with a minimum of N/2 repetition in an array of n elements)

Source: Internet
Author: User
Tags repetition

Title Address:

https://oj.leetcode.com/problems/majority-element/

Topic content:

Given an array of size n, find the majority element. The majority element is the element, the appears more than times ⌊ n/2 ⌋ .

Assume that the array was non-empty and the majority element always exist in the array.

Method:

There are two requirements:

0, time complexity is O (N)

1, Space complexity is O (1)//This is of course, otherwise come a hashmap, who will not = _ =!

The point is to maintain such a nature:

First, let's assume that the target element appears a time, and there is a total of B elements, then:

b/a <= 1/2

We can set two pointers, one pointing to the head and one pointing to the tail. The point is to maintain this property: An array between the pointer and the end, and the number of target elements in the total number of 1/2 . if the two pointers point to an element that is not equal, it means that at least one pointer to the element at this point is not the requested element, so we can discard the two elements, at which point the ratio of the target element in the source array does not degrade.

The proof is relatively simple. Assuming the worst case, one is the target element and the other is not, then there are:

b/a < (b-1)/(a-2), when 2b <= A is established. And 2b <= A is the condition given by the topic.

So what if the two elements are equal? Can we still get rid of these two elements? The answer is no. Because (b-1)/(A-1) < b/a, it is not possible to maintain the property of the target element in the original array without decreasing the ratio.

What should I do then? The answer is to maintain a pointer to the current element "next unequal", and if it is equal, swap the position with the next element that is not equal to yourself. This pointer to the next unequal is only going to go forward, so up to N times. And we maintain a total of 4 pointers, up to O (4n), or O (n).

Here is the specific code:

classSolution { Public:    intMajorityelement (vector<int> &num) {        intStart =0; intFin = num.size ()-1; intSTARTNXT = Findnextstart (Start,num,start);//find the next pointer that is not equal to start.         intFINNXT =Findnextfin (Fin,num,fin);  while(Start <Fin) {            if(Num[start] = =Num[fin]) {                if(STARTNXT >= finnxt)//if the data pointing to the current and the end pointer is equal, then the data between the operations is equal and the result can be returned directly.                     returnNum[start]; Swap (&num[start],&NUM[STARTNXT]); } Start++; Fin--; STARTNXT=Findnextstart (START,NUM,STARTNXT); FINNXT=Findnextfin (FIN,NUM,FINNXT); }        returnNum[start]; }        intFindnextstart (intstart,vector<int> &num,intpreptr) {        intNownumber =Num[start]; intNxtptr = Preptr = = start? Start +1: Preptr;//If the Prestart is just for start, move forward a grid. Otherwise on the basis of the existing foundation.          while(Nxtptr < Num.size () && num[start] = =num[nxtptr]) nxtptr++; returnnxtptr; }        intFindnextfin (intfin,vector<int> &num,intpreptr) {        intNownumber =Num[fin]; intNxtptr = Preptr = = fin? Fin-1: preptr;  while(Nxtptr >=0&& Num[fin] = =num[nxtptr]) nxtptr--; returnnxtptr; }        voidSwapint*a,int*b) {intTMP = *A; *a = *b; *b =tmp; }};

"Original" Leetcodeoj---Majority element problem solving report (a popular search for elements with a minimum of N/2 repetition in an array of n elements)

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.