More test cases are needed


  • 0
    C

    Here is my solution, which passes OJ, I though it's correct until it failed some random test cases of mine, for example, it should return [24] for [51, 24, 24, 43, 24, 38, 32, 9] but returns [] instead.
    Please add more corner cases to this problem:)

    def majorityElement(nums):
        a, b = None, None
        cnt1, cnt2 = 0, 0
        for n in nums:
            if n == a or cnt1 == 0:
                a = n
                cnt1 += 1
            elif n == b or cnt2 == 0:
                b = n
                cnt2 += 1
            else:
                cnt1 -= 1
                cnt2 -= 1
                
            print a, b, cnt1, cnt2
        cnt1 = cnt2 = 0
        for n in nums:
            if n == a:
                cnt1 += 1
            elif n == b:
                cnt2 += 1
            
        return ([a] if cnt1 > len(nums)/3 else [])  + ([b] if cnt2 > len(nums)/3 else [])

Log in to reply
 

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