Three pass solution (probably stupid idea for many experts here)

  • 0
    class Solution(object):
        def thirdMax(self, nums):
            :type nums: List[int]
            :rtype: int
            ninf     = -float("inf")
            firstMax = ninf
            secondMax = ninf
            thirdMax = ninf
            if nums:
                firstMax = max(nums) # O(n) 
            for i in range(len(nums)): # O(n)
                m = max(secondMax,nums[i])
                if m < firstMax:
                    secondMax = m
            if secondMax == ninf:
                secondMax = firstMax
            for i in range(len(nums)): # O(n)
                m = max(thirdMax,nums[i])
                if m < secondMax:
                    thirdMax = m
            if thirdMax == ninf:
                thirdMax = firstMax
            return thirdMax

Log in to reply

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