单调栈的应用.
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: next = [] stack = [] for i in range(len(nums2)): while stack and stack[-1][1] < nums2[i]: next[stack.pop()[0]] = nums2[i] stack.append((i, nums2[i])) next.append(-1) ret = [] for val in nums1: ret.append(next[nums2.index(val)]) return ret