591. Previous Greater Element

EasyStackMonotonic StackStack

Given an array nums, for each element return the nearest strictly greater element to its left, or -1 if none exists. Return the array of answers. The input is JSON {nums}.

Input: JSON {nums}.

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

Examples

Example 1
Input: {"nums":[10,4,2,20,40,12,30]}
Output: [-1,10,4,-1,-1,40,40]
Explanation: Nearest greater to the left of each.
Example 2
Input: {"nums":[5]}
Output: [-1]
Explanation: Nothing to the left.
Example 3
Input: {"nums":[1,2,3,4]}
Output: [-1,-1,-1,-1]
Explanation: Strictly increasing has none to the left.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →