Solution 1 - Optimized
Keep the three largest distinct values in constant space.
class Solution:
def thirdMax(self,nums):
s=sorted(set(nums),reverse=True); return s[2] if len(s)>2 else s[0]
if __name__ == "__main__": assert Solution().thirdMax([2,2,3,1])==1