-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththird.py
More file actions
93 lines (70 loc) · 2.26 KB
/
third.py
File metadata and controls
93 lines (70 loc) · 2.26 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
"""
# Wrong logic - doesnt work for 4 elements
# Pythonic sort - fails for 112
nums = sorted(nums)[::-1]
maxi1 = 0
maxi2 = 0
maxi3 = 0
if len(nums) >= 3:
for num in nums:
if max
return nums[len(nums)-1]
else:
return nums[0]
"""
"""
# Tracking the third max - optimize and improvise, still blocked !!!
maxi1 = -60000000
maxi2 = -60000000
maxi3 = -60000000
for num in sorted(nums)[::-1]:
if num > maxi1:
maxi1 = num
if num > maxi2 and num < maxi1:
maxi2 = num
if num > maxi3 and num < maxi1 and num < maxi2:
maxi3 = num
else:
maxi3 = maxi1
return maxi3
"""
"""
# 100 pass - Have the presence of mind to think properly - Think straight + clear + logical
# **** Use SET to eliminate duplicates
# **** Use SORTED to sort the list
# **** Use MAX to fetch the maximum as well, these were logic you thought off very soon in a clear mind state
# Only unique
nums = list(set(nums))
# Length calculation
n = len(nums)
# Length lesser than 2
if n <=2 :
return max(nums)
else:
# Sorted logic
nums = sorted(nums)
return nums[n-3]
"""
## 100 pass - Hacky Pythonic Max solution
# Eradicate duplicates
nums = list(set(nums))
# Length Calculation
n = len(nums)
# Maximum to return when length is lesser than 3
maximum = max(nums)
# Loop result
result = None
i = 1
if n <= 2:
return maximum
while i <= 3 and n >= 3:
print max(nums), nums
result = nums.pop(nums.index(max(nums)))
i += 1
return result