Most general code using stack without trick and 4-th variant I met on my OA
Algorithm:\nMono Stack as you know\n\n---\nFinal code:\n\njava\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        // corner case\n  
**Algorithm**:\n**Mono Stack** as you know\n\n---\nFinal code:\n\n```java\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        // corner case\n        if(nums == null) return null;\n        \n        int n = nums.length;\n        int[] res = new int[n];\n                \n        Deque<Integer> stack = new ArrayDeque<>(); // to store idx\n        for(int i = 0; i < 2 * n; i++){       \n            int num = nums[i % n];\n            while(!stack.isEmpty() && num > nums[stack.peekFirst()]){\n                int idx = stack.pollFirst();\n                res[idx] = num;\n            }\n            \n            if(i < n) stack.offerFirst(i);\n        }\n        \n        // set the max\'s greater element to be -1\n        while(!stack.isEmpty()){\n            int idx = stack.pollFirst();\n            res[idx] = -1;\n        }\n\n        return res;\n    }\n}\n```\n\n---\nTime complexity: `O(n)`\nSpace complexity: `O(n)` \n- If the array is sorted in descending order like `[5, 4, 3, 2, 1]` or \n- All the elements in the array are the same like `[5, 5, 5, 5, 5]`\n\n---\n**What\'s the trick of other OP?**\n\n```java\n public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length, next[] = new int[n];\n        Arrays.fill(next, -1); // difference!!!\n        Stack<Integer> stack = new Stack<>(); // index stack\n        for (int i = 0; i < n * 2; i++) {\n            int num = nums[i % n]; \n            while (!stack.isEmpty() && nums[stack.peek()] < num)\n                next[stack.pop()] = num;\n            if (i < n) stack.push(i);\n        }   \n\t\t\n\t\t// no polling all nodes in stack and set max\'s greater element to be -1\n\t\t// because of the difference!!!\n\t\t\n        return next;\n    }\n```\n**One trick hidden** if you notice: `Arrays.fill(res, -1);`, have you wondered why we initalize the `res` to `-1` here ?\n**Guess**: according to the definition? \nNope, `-1` means there is no greater number exists, i.e. currrent number is the max number, but initially we just don\'t know if there is a greater number, doesn\'t mean that there is no one.\n**Reason** is that it can help us **save extra operations** to set the greater element of the max number to be `-1`!  As we know, at last there **are** only indices of the max number in the stack. For example: `[1, 5, 3, 5, 4]`, at last the stack will be: `[2, 3]` which is the indices of number `5`. If we initialize them to be `-1`, nothing we need to take care of. \n**Question**: can we do without it?\nYes, we just need to pop all the elements in stack, and set corresponding greater value in `res[]` to be `-1`. It leads to the code below:\n**Compare**: which is better?\nHonestly, I like my own. Since I now have a clear picture of what the whole process is like especially at the end.\n\n---\n**How to deal with circular array? 3 variations to deal with circular array**:\n1. we can scan the array twice using 2 for loop, but will cause redundancy code\n2. we can expand the original array, but can be simplified by method 3\n3. we can simulate expanding the original array like the author\'s code\n\n---\nNote: we push elements to stack in the 1st round, but only pop elements in the 2nd round!!!\n\n---\n**Variant**\n**Closest greater element**\nlike the name, find the the greater element which is most cloest to it, if have 2 elements having same distance to an element, choose the greater element with the smallest index. For example: `[4, 2 ,1, 3]`, return `[-1, 4, 2, 4]`.
['Stack', 'Java']