""" 0414.1 - Solution 1 - Sorting with Set """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def thirdMax(self, nums: List[int]) -> int:
"""Third Maximum Number Function"""
unique = sorted(set(nums), reverse=True)
return unique[2] if len(unique) >= 3 else unique[0]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().thirdMax([3, 2, 1])) # Expected: 1
print(Solution().thirdMax([1, 2])) # Expected: 2
print(Solution().thirdMax([2, 2, 3, 1])) # Expected: 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0414.2 - Solution 2 - Three Variables Tracking """
#####################################################################################
# Imports
#####################################################################################
from typing import List
import math
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def thirdMax(self, nums: List[int]) -> int:
"""Third Maximum Number Function"""
first = second = third = -math.inf
for num in nums:
if num in (first, second, third):
continue
if num > first:
first, second, third = num, first, second
elif num > second:
second, third = num, second
elif num > third:
third = num
return third if third != -math.inf else first
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().thirdMax([3, 2, 1])) # Expected: 1
print(Solution().thirdMax([1, 2])) # Expected: 2
print(Solution().thirdMax([2, 2, 3, 1])) # Expected: 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()