Skip to content

Latest commit

 

History

History
78 lines (64 loc) · 2.17 KB

_414. Third Maximum Number.md

File metadata and controls

78 lines (64 loc) · 2.17 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : May 23, 2024

Last updated : July 01, 2024


Related Topics : Array, Sorting

Acceptance Rate : 36.22 %


Solutions

Python

class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        newNums = sorted(list(set(nums)))
        return newNums[len(newNums) - (3 if len(newNums) >= 3 else 1)]
# A bit needlessly complicated, but does it's job. Might be more efficient just to
# use the max() and min() functions to initially set the 1st and 3rd maxes given the
# added operations for large datasets, but eh.

class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        maxes = []

        for i in nums :
            if i in maxes :
                continue
            if len(maxes) < 3 :
                if (len(maxes) == 0) :
                    maxes.append(i)
                elif len(maxes) == 1 :
                    if i > maxes[0] :
                        maxes.append(i)
                    else :
                        maxes.insert(0, i)
                else :
                    if i > maxes[1] :
                        maxes.append(i)
                    elif i > maxes[0] :
                        maxes.insert(1, i)
                    else :
                        maxes.insert(0, i)
                continue
            
            if i > maxes[2] :
                maxes[0] = maxes[1]
                maxes[1] = maxes[2]
                maxes[2] = i
            elif i > maxes[1] :
                maxes[0] = maxes[1]
                maxes[1] = i
            elif i > maxes[0] :
                maxes[0] = i
        if len(maxes) < 3 :
            return max(maxes)
        else :
            return maxes[0]