608. Next Greater Element II (Circular Array)

MediumStackMonotonic StackStack

Given a circular array nums (the element after the last is the first), return for each element the next strictly greater element when scanning circularly, or -1 if none exists. Return the array of answers. The input is JSON {nums}.

Input: JSON {nums}.

Output: Array — the next greater element for each position.

Examples

Example 1
Input: {"nums":[1,2,1]}
Output: [2,-1,2]
Explanation: The last 1 wraps to the 2.
Example 2
Input: {"nums":[5]}
Output: [-1]
Explanation: Only element.
Example 3
Input: {"nums":[3,2,1]}
Output: [-1,3,3]
Explanation: Smaller elements wrap to 3.

Constraints

Asked by

AmazonFlipkartBloombergGoogleMicrosoftMeta
Solve this problem in the editor →