Nearest Smaller Neighbour
MediumP2Coding
For each element of nums, find the nearest element to its left that is strictly smaller. Return an array with that element for each position, or -1 if there is none.
Nearest means the closest index to the left, not the smallest value.
Example 1
- Input
- nums = [4,5,2,10,8]
- Output
- [-1,4,-1,2,2]
Example 2
- Input
- nums = [3,2,1]
- Output
- [-1,-1,-1]
Example 3
- Input
- nums = [1,2,3]
- Output
- [-1,1,2]
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.