Next Greater Element II

Medium

Topics
ArrayStackMonotonic Stack

Given a circular array nums, return an array where each position holds the next greater element (searching circularly), or -1 if none exists.

Example 1

Input:  nums = [1,2,1]
Output: [2,-1,2]

Example 2

Input:  nums = [1,2,3,4,3]
Output: [2,3,4,-1,4]

Constraints

  • 1 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
Run ⌘' · Submit ⌘⏎