Python #Divide and Conquer##Partition#


  • 0
    C
    class Solution(object):
        def thirdMax(self, nums):
            nums=list(set(nums))
            if len(nums)>=3:
                return self.tMin(nums,3)
            else:
                return self.tMin(nums,1)
        def tMin(self,nums,k):
            pivot = nums[len(nums)//2]
            left=[]
            equal=[]
            right=[]
            for i in nums:
                if i<pivot:
                    left.append(i)
                elif i==pivot:
                    equal.append(i)
                else:
                    right.append(i)
            print equal
            if k <= len(right):
                return self.tMin(right, k)
            elif (k - len(right)) <= len(equal):
                return equal[0]
            else:
                return self.tMin(left, k - len(right) - len(equal))
    

    I used what I have just learnt from my Algorithm course :]


Log in to reply
 

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