mediumStackPattern: Monotonic Stack

Next Smaller Element Indices Solution

Problem Statement

You are given an array of integers `signals` representing signal amplitudes. Find the next smaller signal amplitude for each signal in the sequence and return an array of indices, where each index represents the position of the next smaller signal amplitude.

Examples

Example 1:
Input:[5, 2, 3, 1, 4]
Output:[1, -1, 3, -1, -1]
Explanation: For the input [5, 2, 3, 1, 4], the next smaller signal amplitude for each signal is: 2 (at index 1) for 5, no smaller amplitude for 2, 1 (at index 3) for 3, no smaller amplitude for 1, and no smaller amplitude for 4

Constraints

  • The input number is a string of digits.
  • The k value can vary from 1 to the number of digits in the input.
Time: O(n + k) Space: O(n)
Use a stack to find the next smaller signal amplitude in O(n) time complexity

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.