> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/stack/496.-next-greater-element-i.md).

# 496. Next Greater Element I!

```java
class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        // stack, smaller number on the top,
        // if push a larger one, pop the small one out
        Map<Integer, Integer> memo = new HashMap<>();
        Deque<Integer> stack = new LinkedList<>();
        
        for (int num : nums2){
            while(stack.size()>0 && stack.peek()<num){
                // num is the next greater element of stack.pop();
                memo.put(stack.pop(), num);
            }
            stack.push(num);
        }
        
        for(int i = 0; i<nums1.length; i++){
            nums1[i]=memo.getOrDefault(nums1[i],-1);
        }
        return nums1;
    }
}
```
