169. Majority Element (python) __python

Source: Internet
Author: User

Given an array of size n, find the majority element. The majority element is the element this appears more than⌊n/2⌋times.
You may assume this array is non-empty and the majority element always exist in the array.
Given an array of length n, find the main element, which appears in the array more than One-second of the number of elements in the array.
Method I: Violent search method, time complexity O (n^2), Space complexity O (1)
But the method will appear: Time Limit exceeded
Method Two: After sorting the ⌊n/2⌋ element is the main element, but the premise is that the element does exist, this method is valid
Runtime:75 ms
Fast sorting method Time complexity O (NLOGN), Space complexity of O (1)
The basic idea of fast sorting is to divide the data into two separate parts by a sort of sorting. A part of all the data is smaller than the other part of all the data, and then the two parts of the data for the rapid sorting, the entire sequencing process can be recursive, so as to achieve the entire data into an ordered sequence.
Method Three: Hash table, {Key:value}-{element: occurrences}
runtime:135 ms
Method Four: Moore ' s voting algorithm, time complexity is O (n), space complexity is O (1)
Runtime:86 ms
1 each time find a pair of different elements, from the array deleted, until the array is empty or only one element. It is not difficult to prove that if there is more than half the frequency of element E, then the last remaining in the array is only E.
2 to check whether the elements obtained from the above is the main element (to avoid the absence of the main element, it needs to be judged)
Implementation: Iterates through each element and maintains a counter count for the element A[maj_index], if the element is the same as the previous element, Count plus 1, and if not, count minus 1. If count is equal to 0, then assign the index I of the next element to Maj_index and set count to 1.
Reference: http://blog.csdn.net/shinanhualiu/article/details/50410313
Moore's voting algorithm, given an array B of size n, finds elements in array B that have more than n/k occurrences. An element that appears more than n/k in array B is at most k−1 a different element. It is easy to prove the problem.
Method One:

Class Solution (object):
    def majorityelement (self, nums):
        If Len (nums) ==0: Return
            None to
        I in range ( Len (nums)):
            Num=nums[i]
            count=0 for
            J in Range (Len (nums)):
                if nums[j]==nums[i]:
                    count+=1
            If Count>len (nums)/2: Return
                Nums[i]

Method Two:

Class Solution (object):
    def majorityelement (self, nums):
        nums.sort () return
        Nums[len (nums)/2]

Method Three:

Class Solution (object):
    def majorityelement (self, nums):
        dict={} for
        I in range (len (nums)):
            If Not nums[i] in dict:
                dict[nums[i]]=1
            Else:
                dict[nums[i]]+=1
            if  dict[nums[i]]>len (nums )/2:
                return  Nums[i]      

Method Four:

class Solution (object): Def majorityelement (self, nums): count=0 maj=nums[0]
                For I in range (len (nums)): If maj==nums[i]: count+=1 Else:
            Count-=1 if count==0:maj=nums[i+1] c=0 for I in range (len (nums)): If Nums[i]==maj:c+=1 if C>len (nums)/2:return Maj 

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.