Track arriving and leaving people to determine the maximum number of chairs needed at any moment.
Minimum Number of Chairs in a Waiting Room
You are given a string representing a sequence of events in a waiting room. Each character describes what happens at one step:
Emeans one person enters the room.Lmeans one person leaves the room.
A chair is needed for each person currently inside the room. Your task is to determine the minimum number of chairs that must be available so that everyone who is in the room at the same time has a chair.
In other words, compute the maximum number of people present in the room at any point while processing the events from left to right.
Notes
- Assume the room starts empty.
- The sequence is valid, so the number of people leaving never exceeds the number currently inside.
- You only need to return the minimum chairs required, not the timeline.
Input Format
- A single string
eventsconsisting only of the charactersEandL.
Each character represents one event in order from left to right.
Output Format
- Return a single integer: the minimum number of chairs required in the waiting room.
Constraints
events[i]is eitherEorL- The event sequence is valid
Example 1
Input
events = "EELLE"
Output
2
Explanation
The occupancy changes as 0 → 1 → 2 → 1 → 0 → 1, so the maximum number of people present at once is 2. That means 2 chairs are enough.
Example 2
Input
events = "ELELEL"
Output
1
Explanation
The room never has more than one person at a time, so only 1 chair is needed.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.