Python using priority queue


  • 0
    L
    import heapq
    class Solution(object):
        def thirdMax(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            heap=[]
            sets=set()
            for i in nums:
                if i not in sets:
                    sets.add(i)
                    heapq.heappush(heap, i)
            if len(heap)<3:
                return heap[-1]
            return heapq.nlargest(3,heap)[-1]

Log in to reply
 

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