def next_greater_element(nums1, nums2): stack = [] map = {} # iterate over nums2 for current in nums2: # while stack is not empty and current element is greater than the top element of the stack while stack and current > stack[-1]: # update the map with the current element as the value for the popped element map[stack.pop()] = current # push the current element to the stack stack.append(current) # iterate over remaining elements in the stack, pop them and set their values to -1 in the map while stack: map[stack.pop()] = -1 ans = [] # iterate over nums1 and add the corresponding value from the map to ans for num in nums1: ans.append(map[num]) return ans def main(): A = [[2, 4], [3, 2, 5], [14, 45, 52], [1, 3, 2], [4, 2], [0]] B = [[1, 2, 3, 4], [2, 3, 5, 1], [52, 14, 45, 65], [1, 3, 2, 4, 5], [1, 2, 4, 3], [0]] x = 1 for i in range(len(A)): print(x, ".\tNums 1 = ", A[i], sep="") print("\tNums 2 = ", B[i], sep="") print("") print("\tThe Next Greater Element Array = ", next_greater_element(A[i], B[i])) print(100 * '-') x += 1 if __name__ == '__main__': main()