from typing import List class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: res = [] num2_dict = {} for i in range(len(nums2)): num2_dict[nums2[i]] = i for x in nums1: exist = False for j in range(num2_dict[x], len(nums2)): if nums2[j] > x: res.append(nums2[j]) exist = True break if not exist: res.append(-1) return res if __name__ == '__main__': print(Solution().nextGreaterElement([4, 1, 2], [1, 3, 4, 2]))