STAQ-P5

Next taller signal

Practice 5 of 6Stacks and queues10 tests

The problem

Signals stand in a row, and a[i] is the height of signal i. For each signal, find the first signal to its right that is strictly taller, and give its height. If there is none, give -1. Return the answers as a new list.

next_greater([2, 7, 3, 5, 4, 6, 1])   # [7, -1, 5, 6, 6, -1, -1]
next_greater([3, 3, 3])               # [-1, -1, -1]

Limits

  • Up to 200,000 signals. Your solution should be O(n).

Builds on STAQ-5 Monotonic stack.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

10 tests, not run yet
Test results
#TestResultReads · time
1The lesson's listNot run
2Equal heightsNot run
3Going upNot run
4Going downNot run
5EmptyNot run
6OneNot run
7Answer far awayNot run
8Input not changedNot run
9200,000 going downNot run
10200,000 mixedNot run