Simplest Java Code 🏁🚀🧑‍💻
Intuition\nNext Greater Element IV\n\n# Approach\n\n\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\njava []\nclass Solution {\n
# Intuition\nNext Greater Element IV\n\n# Approach\n![Screenshot 2024-12-02 174611.png](https://assets.leetcode.com/users/images/e58a3c33-e6ec-4f19-a0a4-3c945c523a7c_1733141804.413679.png)\n\n\n# Complexity\n- Time complexity: ***$$O(n)$$***\n\n- Space complexity: ***$$O(n)$$***\n\n# Code\n```java []\nclass Solution {\n    public int[] secondGreaterElement(int[] nums) {\n        Stack<Integer> st1 = new Stack<>();\n        Stack<Integer> st2 = new Stack<>();\n        Stack<Integer> temp = new Stack<>();\n\n        int[] result = new int[nums.length];\n        Arrays.fill(result, -1);\n        for(int i = 0; i < nums.length; i++){\n            while(!st2.isEmpty() && nums[i] > nums[st2.peek()]){\n                result[st2.pop()] = nums[i];\n            } \n            while(!st1.isEmpty() && nums[i] > nums[st1.peek()]){\n                temp.push(st1.pop());\n            }\n            while(!temp.isEmpty()){\n                st2.push(temp.pop());\n            }\n\n            st1.push(i);\n        }\n\n        return result;\n    }\n}\n```
['Array', 'Stack', 'Java']