forked from alqamahjsr/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path414_Third_Maximum_Number.py
46 lines (32 loc) · 1.37 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
45
46
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
threeLargest = [None, None, None]
for num in nums:
self.updateThreelargest(threeLargest, num)
if threeLargest[0] is None:
return threeLargest[2]
else:
return threeLargest[0]
def updateThreelargest(self, threeLargest, num):
if threeLargest[2] is None or num > threeLargest[2]:
self.shiftAndUpdateLargest(threeLargest, num, 2)
elif threeLargest[1] is None or num > threeLargest[1]:
self.shiftAndUpdateLargest(threeLargest, num, 1)
elif threeLargest[0] is None or num > threeLargest[0]:
self.shiftAndUpdateLargest(threeLargest, num, 0)
def shiftAndUpdateLargest(self, threeLargest, num, index):
if num in threeLargest:
return
# Since the index/range is exclusive in python, so we need to add 1 here. For example, if we put range(2), then the value will be 0 to 1, exlcuding last range. So we need to add 1 here to include the last index into the range.
for i in range(index + 1):
if i == index:
threeLargest[index] = num
else:
threeLargest[i] = threeLargest[i + 1]
obj = Solution()
index = obj.thirdMax([2, 2, 3, 1])
print("Max: ", index)