Next Greater Element II


I think first adding all element indices to stack is easier to understand for me.
public int[] nextGreaterElements(int[] nums) { int n = nums.length; Stack<Integer> stack = new Stack(); for(int i = n  1; i >= 0; i){ stack.push(i); } int[] res = new int[n]; for(int i = n  1; i >= 0; i){ res[i] = 1; while(!stack.isEmpty() && nums[stack.peek()] <= nums[i]){ stack.pop(); } if(!stack.isEmpty()) res[i] = nums[stack.peek()]; stack.push(i); } return res; }