单调栈的应用.

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

 

相关文章:

  • 2021-11-18
  • 2022-02-17
  • 2021-12-03
  • 2021-08-29
  • 2022-12-23
猜你喜欢
  • 2021-06-22
  • 2021-11-27
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2021-05-18
相关资源
相似解决方案