[Java/C++/Python] Loop Twice
Explanation\nLoop once, we can get the Next Greater Number of a normal array.\nLoop twice, we can get the Next Greater Number of a circular array\n\n\n## Comple
## **Explanation**\nLoop once, we can get the Next Greater Number of a normal array.\nLoop twice, we can get the Next Greater Number of a circular array\n<br>\n\n## **Complexity**\nTime `O(N)` for one pass\nSpce `O(N)` in worst case\n<br>\n\n**Java**\n```java\n    public int[] nextGreaterElements(int[] A) {\n        int n = A.length, res[] = new int[n];\n        Arrays.fill(res, -1);\n        Stack<Integer> stack = new Stack<>();\n        for (int i = 0; i < n * 2; i++) {\n            while (!stack.isEmpty() && A[stack.peek()] < A[i % n])\n                res[stack.pop()] = A[i % n];\n            stack.push(i % n);\n        }\n        return res;\n    }\n```\n**C++:**\n```cpp\n    vector<int> nextGreaterElements(vector<int>& A) {\n        int n = A.size();\n        vector<int> stack, res(n, -1);\n        for (int i = 0; i < n * 2; ++i) {\n            while (stack.size() && A[stack.back()] < A[i % n]) {\n                res[stack.back()] = A[i % n];\n                stack.pop_back();\n            }\n            stack.push_back(i % n);\n        }\n        return res;\n    }\n```\n\n**Python:**\n```py\n    def nextGreaterElements(self, A):\n        stack, res = [], [-1] * len(A)\n        for i in range(len(A)) * 2:\n            while stack and (A[stack[-1]] < A[i]):\n                res[stack.pop()] = A[i]\n            stack.append(i)\n        return res\n```\n<br>\n\n# More Good Stack Problems\nHere are some problems that impressed me.\nGood luck and have fun.\n\n[1130. Minimum Cost Tree From Leaf Values](https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/discuss/339959/One-Pass-O(N)-Time-and-Space)\n[907. Sum of Subarray Minimums](https://leetcode.com/problems/sum-of-subarray-minimums/discuss/170750/C++JavaPython-Stack-Solution)\n[901. Online Stock Span](https://leetcode.com/problems/online-stock-span/discuss/168311/C++JavaPython-O(1))\n[856. Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/discuss/141777/C++JavaPython-O(1)-Space)\n[503. Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/discuss/98270/JavaC++Python-Loop-Twice)\n496. Next Greater Element I\n84. Largest Rectangle in Histogram\n42. Trapping Rain Water\n
[]