Solution to LeetCode Next Greater Element Ⅰ in Python
Since list one is a subset of list two, iterating over two lists can be avoided. This can be achieved by finding the next greater element of each element for the bigger list and storing the result in a hash table.

To calculate the next greater element of each element in a list we can iterate the list. For each element, we can find the next greater element by again iterating the list. This approach is simple but will have O(n^2) complexity.

Another approach is by using stack. 


First, push the first element of the list int the stack. 
Now iterate the list and compare the top element of the stack with the current element of the list. 
If the current element is greater than the top element then that means the current element is the next greater element of the top element of the stack. Store this result in a dictionary. Pop the top element out of the stack. 
Repeat above two steps until the stack top element is greater than the current element or stack is empty. 
At the end of each iteration push the current element in the stack.


The code will make things clearer: 


def calculate_nge_dict(arr):

    nge_dict = {}
    stack_ = []
    stack_.append(arr[0])
    nge_dict[arr[0]] = -1
    for i in arr[1:]:
        next_ = i
        nge_dict[next_] = -1
        while stack_:
            top_ = stack_.pop()
            if next_ &lt;= top_:
                stack_.append(top_)
                break
            else:
                nge_dict[top_] = next_
        stack_.append(next_)
    return nge_dict

def next_greater_element(nums1, nums2):
    nge_dict = calculate_nge_dict(nums2)
    return [nge_dict[i] for i in nums1]



You can get a better understanding of the algorithm from this answer.