592. Previous Smaller Element

EasyStackMonotonic StackStack

Given an array nums, for each element return the nearest strictly smaller 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 smaller element for each position.

Examples

Example 1
Input: {"nums":[1,6,4,10,2,5]}
Output: [-1,1,1,4,1,2]
Explanation: Nearest smaller to the left of each.
Example 2
Input: {"nums":[5]}
Output: [-1]
Explanation: Nothing to the left.
Example 3
Input: {"nums":[4,3,2,1]}
Output: [-1,-1,-1,-1]
Explanation: Strictly decreasing has none to the left.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →