Algobase
Problems
Get Premium
Pricing
Problems
/
274. Next Greater Element I
Prev
Next
Visualizer
Problem
Solution
Code
Mixed Values
Decreasing Values
Increasing Values
⌥
Visualizer will appear here
Problem
Solution
Code
1
def next_greater(nums1, nums2):
2
mapping = {}; stack = []
3
for num in nums2:
4
while stack and stack[-1] < num:
5
popped = stack.pop()
6
mapping[popped] = num
7
stack.append(num)
8
return [mapping.get(n, -1) for n in nums1]
Visualizer
Mixed Values
Decreasing Values
Increasing Values
⌥
Visualizer will appear here