Python Solution beats 99%

  • 0
    class Solution(object):
        def majorityElement(self, nums):
            ans = []
            l = len(nums)
            nums_set = set(nums)
            for n in nums_set:
                if nums.count(n) > l / 3:
            return ans

  • 1

    Doesn't that have a worst case Space complexity of O(n) if no number is repeated ?
    (num_set would basically be the same size as nums if there's no repeated number, so it will not be constant space)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.