Skip to content

Commit 0f68d77

Browse files
authored
Create neither-minimum-nor-maximum.py
1 parent 288921a commit 0f68d77

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Python/neither-minimum-nor-maximum.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# one pass, array
5+
class Solution(object):
6+
def findNonMinOrMax(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
mx, mn = float("-inf"), float("inf")
12+
result = -1
13+
for x in nums:
14+
if mn < x < mx:
15+
return x
16+
if x < mn:
17+
result = mn
18+
mn = x
19+
if x > mx:
20+
result = mx
21+
mx = x
22+
return result if mn < result < mx else -1
23+
24+
25+
# Time: O(n)
26+
# Space: O(1)
27+
# array
28+
class Solution2(object):
29+
def findNonMinOrMax(self, nums):
30+
"""
31+
:type nums: List[int]
32+
:rtype: int
33+
"""
34+
mx, mn = max(nums), min(nums)
35+
return next((x for x in nums if x not in (mx, mn)), -1)

0 commit comments

Comments
 (0)