-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy path132-pattern.py
43 lines (41 loc) · 1.08 KB
/
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
38
39
40
41
42
43
# V0
# V1
# https://blog.csdn.net/u011693064/article/details/54650260
# https://blog.csdn.net/fuxuemingzhu/article/details/81869398
class Solution:
def find132pattern(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if len(nums) <=2:
return False
third = float('-inf')
stack = []
for i in range(len(nums)-1, -1, -1):
if nums[i] < third:
return True
else:
while stack and stack[-1] < nums[i]:
third = stack.pop()
stack.append(nums[i])
return False
# V2
# Time: O(n)
# Space: O(n)
class Solution(object):
def find132pattern(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
ak = float("-inf")
st = []
for i in reversed(xrange(len(nums))):
if nums[i] < ak:
return True
else:
while st and nums[i] > st[-1]:
ak = st.pop()
st.append(nums[i])
return False