> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/04-29-2022.md).

# 04/29/2022

```
// Some code
class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int[] result = new int[nums.length];
        Arrays.fill(result, -1);
        for (int i = 0; i < nums.length; i++){
            int index = i + 1;
            for (int j = 1; j < nums.length; j++){
                if (i + j == nums.length) index = 0;
                if (nums[index] > nums[i]){
                    result[i] = nums[index];
                    break;   
                } 
                index++;
                
            }
        }
        return result;
    }
}
```

```
// Some code
class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int[] result = new int[nums.length];
        Arrays.fill(result, -1);
        Stack<Integer> stack = new Stack<>();
        for (int i = nums.length - 1; i >= 0; i--){
            stack.push(nums[i]);
        }
        
        for (int i = nums.length - 1; i >= 0; i--){
            while (!stack.isEmpty() && nums[i] >= stack.peek()){
                stack.pop();
            }
            result[i] = stack.isEmpty()? -1: stack.peek();
            stack.push(nums[i]);
        }
        
        return result;
    }
}
```
