Skip to content

Commit 300d720

Browse files
committed
find third max
1 parent 0c5534a commit 300d720

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

third_max.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
class Solution(object):
3+
def thirdMax(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: int
7+
"""
8+
first_max = -sys.maxsize
9+
second_max =-sys.maxsize
10+
third_max = -sys.maxsize
11+
for i in nums:
12+
if(i> first_max):
13+
third_max = second_max
14+
second_max = first_max
15+
first_max = i
16+
elif i == first_max:
17+
pass
18+
elif i > second_max:
19+
third_max = second_max
20+
second_max = i
21+
elif i == second_max:
22+
pass
23+
elif i> third_max:
24+
third_max = i
25+
print(third_max,second_max,first_max)
26+
if third_max == -sys.maxsize:
27+
return first_max
28+
else:
29+
return third_max
30+
31+
sol = Solution()
32+
print(sol.thirdMax([1,2]))
33+
print(sol.thirdMax([3,3, 2, 1]))
34+
print(sol.thirdMax([3, 2,2, 1]))
35+
print(sol.thirdMax([3, 2, 1]))
36+

0 commit comments

Comments
 (0)