Taller Friend at the Round Table
Medium · Stack & Monotonic Stack · about 20 min
Friends sit around a round table. You get their heights in centimetres, in clockwise order; after the last person comes the first one again.
For every person, look clockwise (to higher indexes, wrapping around to the start) and find the first friend who is strictly taller. Return a list with that friend's height for each person, or -1 if nobody at the table is taller.
Example: round_table_taller([150, 170, 160]) → [170, -1, 170] (the person with 160 wraps around past the end and finds 150, then 170)
Constraints: 0 to 100,000 friends; heights are between 1 and 300.
Hints
- First solve it for a straight row: which people are still waiting for someone taller, and who answers them?
- Keep a stack of waiting people (their indexes). A new person answers every waiting person who is strictly shorter.
- For the circle, walk the indexes twice using the remainder by n. Only push people during the first lap.
Learn the pattern first: the Stack & Monotonic Stack lesson.
More Stack & Monotonic Stack problems
- Config Bracket Check (Easy)
- Cancelling Beads (Easy)
- Calculator Tape (Easy)
- Lightest Parcel on the Pile (Medium)
- Banner Expander (Medium)
- Scooter Convoys (Medium)
- Biggest Skyline Billboard (Hard)