-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path414 Third Maximum Number.py
44 lines (37 loc) · 1.13 KB
/
414 Third Maximum Number.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python3
"""
Given a non-empty array of integers, return the third maximum number in this
array. If it does not exist, return the maximum number. The time complexity
must be in O(n).
Author: Rajeev Ranjan
"""
import heapq
class Solution:
def thirdMax(self, nums):
"""
It is an easy question but error prone:
1. Choice of min heap or max heap: use min heap (not max heap) because
we want to know the smallest maximum number
2. Duplicate number
:type nums: List[int]
:rtype: int
"""
if not nums:
return None
h = []
for e in set(nums):
if len(h) < 3:
heapq.heappush(h, e)
elif len(h) == 3 and e > h[0]:
heapq.heappushpop(h, e)
assert len(h) <= 3
if len(h) == 3:
ret = min(h)
else:
ret = max(h)
return ret
if __name__ == "__main__":
assert Solution().thirdMax([1, 2, 3, 4]) == 2
assert Solution().thirdMax([4, 3, 2, 1]) == 2
assert Solution().thirdMax([2, 2, 3, 1]) == 1
assert Solution().thirdMax([4, 3]) == 4