Easy
Next Greater Element I — Python
Full explanation · Time O(m + n) · Space O(m + n)
# Time: O(m + n)
# Space: O(m + n)
class Solution(object):
def nextGreaterElement(self, findNums, nums):
"""
:type findNums: List[int]
:type nums: List[int]
:rtype: List[int]
"""
stk, lookup = [], {}
for num in nums:
while stk and num > stk[-1]:
lookup[stk.pop()] = num
stk.append(num)
while stk:
lookup[stk.pop()] = -1
return map(lambda x : lookup[x], findNums)