Next Smaller Element
Given an integer array nums, return an array where answer[i] is the nearest value strictly smaller than nums[i] among positions to its right. Return -1 when…
Given an integer array `nums`, return an array where `answer[i]` is the nearest value strictly smaller than `nums[i]` among positions to its right. Return `-1` when no strictly smaller value exists to the right.
- `1 <= nums.length <= 100000` - `-1000000000 <= nums[i] <= 1000000000` - Equal values are not strictly smaller.
Example 1: input nums = [4,8,5,2,25]; output [2,5,2,-1,-1]. The nearest smaller value after 8 is 5, while 4 reaches 2 after skipping larger values.
Example 2: input nums = [2,2,1,2]; output [1,1,-1,-1]. Equal 2 values do not qualify as smaller, so each leading 2 reaches 1 instead.
Category: Stack. Topic: stack. Difficulty: Medium. Patterns: monotonic-stack.