-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe_third_largest.py
More file actions
27 lines (22 loc) · 1003 Bytes
/
Copy pathe_third_largest.py
File metadata and controls
27 lines (22 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
############################################################
# LeetCode Problem Number : 414
# Difficulty Level : Easy
# URL : https://leetcode.com/problems/third-maximum-number/
############################################################
from typing import List
class ThirdLargestNumber:
# runtime -> 97.90%, memory -> 83.47%
# complexity -> time O(n), space O(1)
def find(self, nums: List[int]) -> int:
if len(nums) < 3:
return max(nums)
temp_list = [float("-inf"), float("-inf"), float("-inf")]
for num in nums:
if num not in temp_list:
if num > temp_list[0]:
temp_list = [num, temp_list[0], temp_list[1]]
elif num > temp_list[1]:
temp_list = [temp_list[0], num, temp_list[1]]
elif num > temp_list[2]:
temp_list = [temp_list[0], temp_list[1], num]
return max(temp_list) if float("-inf") in temp_list else temp_list[2]