Java use a stack
More concise version:\n\n    public int[] nextGreaterElements(int[] nums) {\n        Stack<Integer> st = new Stack<>();\n        int[] res = new int[nums.length
More concise version:\n```\n    public int[] nextGreaterElements(int[] nums) {\n        Stack<Integer> st = new Stack<>();\n        int[] res = new int[nums.length];\n        for (int cnt = 0; cnt < 2; cnt++) {\n            for (int i = 0; i < nums.length; i++) {\n                while (!st.isEmpty() && nums[i] > nums[st.peek()]) res[st.pop()] = nums[i];\n                if (cnt == 0) st.push(i);\n            }\n        }\n        while (!st.isEmpty()) res[st.pop()] = -1;\n        return res;\n    }\n```
[]