Nothing Smaller To The Left
For each element, report the nearest element to its left that is strictly smaller, or a dash when none exists. The algorithm scans left to right, pops while the stack top is at least the current value, reads the answer off the top, then pushes the current value.
Array: 1 3 4 2 5 3 4 2
| after index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| stack, bottom to top | 1 | 1 3 | 1 3 4 | 1 2 | 1 2 5 | 1 2 3 | ? | ? |
What is the full output?
Sign in to answer questions and track your progress
Sign In