-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
132-pattern.py
37 lines (34 loc) · 903 Bytes
/
132-pattern.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
# Time: O(n)
# Space: O(n)
class Solution(object):
def find132pattern(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
ak = float("-inf")
stk = []
for i in reversed(xrange(len(nums))):
if nums[i] < ak:
return True
while stk and stk[-1] < nums[i]:
ak = stk.pop()
stk.append(nums[i])
return False
# Time: O(n^2)
# Space: O(1)
class Solution_TLE(object):
def find132pattern(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
for k in xrange(len(nums)):
valid = False
for j in xrange(k):
if nums[j] < nums[k]:
valid = True
elif nums[j] > nums[k]:
if valid:
return True
return False