Java Damn Easy to Understand
Time Complexity : O(n)\nSpace Complexity : O(n)\n\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        \n        Stack<Integer> stack=
**Time Complexity : O(n)**\n**Space Complexity : O(n)**\n```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        \n        Stack<Integer> stack=new Stack<>();\n        for(int i=nums.length-1;i>=0;i--){\n            stack.push(nums[i]);\n        }\n        \n        for(int i=nums.length-1;i>=0;i--){\n            int number=nums[i];\n                while(!stack.isEmpty() && stack.peek()<=nums[i]){\n                    stack.pop();\n                }\n           \n            nums[i]=stack.empty()?-1:stack.peek();\n            stack.push(number);\n        }\n        \n        return nums;\n    }\n}\n```\n\n**Time Complexity : O(n)**\n**Space Complexity : O(n)**\n```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        \n        Stack<Integer> stack=new Stack<>();\n        for(int i=nums.length-1;i>=0;i--){\n            stack.push(nums[i]);\n        }\n        \n        int greater[]=new int[nums.length];\n        for(int i=nums.length-1;i>=0;i--){\n            while(!stack.isEmpty() && stack.peek()<=nums[i]){\n                    stack.pop();\n                }\n            greater[i]=stack.empty()?-1:stack.peek();\n            stack.push(nums[i]);\n        }\n        \n        return greater;\n    }\n}\n```
['Stack', 'Java']