任务描述
思路
- 单调栈
代码实现
class Solution:
def nextGreaterElement(self, nums1, nums2):
stack=[]
map={}
for x in reversed(nums2):
while len(stack) != 0 and stack[-1]<x:
stack.pop()
map[x] = -1 if len(stack)==0 else stack[-1]
stack.append(x)
ans = []
for x in nums1:
ans.append(map[x])
return ans