Next Greater Element II Java Solution
\tclass Solution {\n\t\tpublic int[] nextGreaterElements(int[] nums) {\n\t\t\tint len = nums.length;\n\t\t\tint[] res = new int[len];\n\t\t\tArrays.fill(res, -1
\tclass Solution {\n\t\tpublic int[] nextGreaterElements(int[] nums) {\n\t\t\tint len = nums.length;\n\t\t\tint[] res = new int[len];\n\t\t\tArrays.fill(res, -1);\n\t\t\tStack<Integer> stack = new Stack<>();\n\n\t\t\tfor (int i = 0; i < len * 2; i++) {\n\t\t\t\tint num = nums[i % len];\n\t\t\t\twhile (!stack.isEmpty() && nums[stack.peek()] < num) {\n\t\t\t\t\tres[stack.pop()] = num;\n\t\t\t\t}\n\t\t\t\tif (i < len) stack.push(i);\n\t\t\t}\n\n\t\t\treturn res;\n\t\t}\n\t}
[]