[
stack
monotonic deque
]
BinarySearch 0458 Small Large Medium
Problem statement
https://binarysearch.com/problems/Small-Large-Medium/
Solution
Equal to Leetcode 0456. 132 Pattern.
Complexity
Here is O(n)
time/space solution.
Code
class Solution:
def solve(self, nums):
min_list = list(accumulate(nums, min))
stack, n = [], len(nums)
for j in range(n-1, -1, -1):
if nums[j] > min_list[j]:
while stack and stack[-1] <= min_list[j]:
stack.pop()
if stack and stack[-1] < nums[j]:
return True
stack.append(nums[j])
return False