Lintcode:majority number II Problem solving report

Source: Internet
Author: User

Majority number II

Original title Link: http://lintcode.com/en/problem/majority-number-ii/#

Given an array of integers, the majority number is the number of this occurs more than 1/3 of the size of the AR Ray.

Find it.

Note

There is only one majority number in the array

Example

For [1, 2, 1, 2, 1, 3, 3] return 1

Challenge

O (n) time and O (1) space

Solution 1:

Similar to Majority number 1. But we're going to keep 2 number.

1. When encountering the first different value, first record number 2.

2. The new value is different from the n1,n2, then the Cnt1,cnt2 is reduced

3. When n1,n2 any one is 0 o'clock, pick a record from the new value.

4. Finally, the 2 candidate values are checked and the final solution is obtained.

Homepage June actually also want to not quite understand this topic why this solution.

Let's just give it an example.

7 1 7 7 61 61 61 10 10 10 61

N1 7 7 7 7 7 7 7 7 7 10 10

Cnt1 1 1 2 3 2 2 2 1 0 1 1

N2 0 1 1 1 1 61 61 61 61 61 61

Cnt2 0 1 1 1 0 1 2 1 0 0 1

Proof: The condition to cnt1--is: Encounter 2 different values from N1

1. When N2 is empty, the first one will fill the N2 and the second will reduce the cnt1.

2. Or, N2 has been filled, it will be reduced cnt1, but Cnt2 will also be reduced, when Cnt2 reduced to 0, and then meet the new value will not be reduced again. In other words, the continuous reduction of n is always preceded by a continuous

Add over N2 n times (that n times even add N2, will not reduce cnt1).

Above: 2 different values from N1 will be reduced to cnt1. If N1 is a majority value, it must eventually stay.

The above theory is also applicable to N2.

1  Public classSolution {2     /**3      * @paramnums:a List of integers4      * @return: The majority number that occurs more than 1/35      */6      Public intMajoritynumber (arraylist<integer>nums) {7         //Write your code8         //When there is only 1 or 2 elements in the array,9         //there is no solution.Ten         if(Nums = =NULL|| Nums.size () <= 2) { One             return-1; A         } -          -         intN1 = 0; the         intN2 = 0; -          -         intCnt1 = 0; -         intCnt2 = 0; +          -         intSize =nums.size (); +          for(inti = 0; i < size; i++) { A             intnum =Nums.get (i); at             if(Cnt1! = 0 && num = =N1) { -cnt1++; -}Else if(Cnt2! = 0 && num = =n2) { -cnt2++; -}Else if(Cnt1 = = 0) { -Cnt1 = 1; inN1 =num; -}Else if(Cnt2 = = 0) { toCnt2 = 1; +N2 =num; -}Else { thecnt1--; *cnt2--; $             }Panax Notoginseng         } -          the         //count the candiates. +Cnt1 = 0; ACnt2 = 0; the          for(intnum:nums) { +             if(num = =N1) { -cnt1++; $}Else if(num = =n2) { $cnt2++; -             } -         } the          -         if(Cnt1 <Cnt2) {Wuyi             returnN2; the         }         -          Wu         returnN1; -     } About}
View CodeGITHUB:

Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/math/MajorityNumber2.java

Lintcode:majority number II Problem solving report

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.