Search⌘ K
AI Features

Solution: Next Greater Element II

Let's solve the Next Greater Element II problem using the Stacks pattern.

Statement

Given a circular integer array nums where the next element of nums[nums.length - 1] wraps around to nums[0], return an array containing the next greater element for each element in nums.

The next greater element of an element nums[i] is the first element that is strictly greater than nums[i], found by traversing the array in order starting from nums[i]'s position, with circular wraparound allowed. If no such element exists, return 1-1 for that position.

Constraints:

  • 11 \leq nums.length 104\leq 10^4 ...