Watch 10 video solutions for Minimum Number of Chairs in a Waiting Room, a easy level problem involving String, Simulation. This walkthrough by Programming Live with Larry has 335 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s. Simulate events at each second i:
s[i] == 'E', a person enters the waiting room and takes one of the chairs in it.s[i] == 'L', a person leaves the waiting room, freeing up a chair.Return the minimum number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially empty.
Example 1:
Input: s = "EEEEEEE"
Output: 7
Explanation:
After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.
Example 2:
Input: s = "ELELEEL"
Output: 2
Explanation:
Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.
| Second | Event | People in the Waiting Room | Available Chairs |
|---|---|---|---|
| 0 | Enter | 1 | 1 |
| 1 | Leave | 0 | 2 |
| 2 | Enter | 1 | 1 |
| 3 | Leave | 0 | 2 |
| 4 | Enter | 1 | 1 |
| 5 | Enter | 2 | 0 |
| 6 | Leave | 1 | 1 |
Example 3:
Input: s = "ELEELEELLL"
Output: 3
Explanation:
Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.
| Second | Event | People in the Waiting Room | Available Chairs |
|---|---|---|---|
| 0 | Enter | 1 | 2 |
| 1 | Leave | 0 | 3 |
| 2 | Enter | 1 | 2 |
| 3 | Enter | 2 | 1 |
| 4 | Leave | 1 | 2 |
| 5 | Enter | 2 | 1 |
| 6 | Enter | 3 | 0 |
| 7 | Leave | 2 | 1 |
| 8 | Leave | 1 | 2 |
| 9 | Leave | 0 | 3 |
Constraints:
1 <= s.length <= 50s consists only of the letters 'E' and 'L'.s represents a valid sequence of entries and exits.Problem Overview: You receive a string representing events in a waiting room. Each character indicates whether a person enters or leaves. The task is to compute the minimum number of chairs required so that every person who enters always has a seat available.
Approach 1: Simulate Each Event Using Two Counters (O(n) time, O(1) space)
This method directly simulates the waiting room activity using counters. Iterate through the string and update the number of people currently sitting whenever someone enters or leaves. When an entry occurs, increase the current occupancy and update the maximum occupancy seen so far. When a person leaves, decrease the counter. The maximum occupancy at any point equals the minimum number of chairs required. This approach models the system exactly as described and is easy to reason about when dealing with string-based event logs and simple simulation problems.
Approach 2: Optimized with Incremental Update (O(n) time, O(1) space)
The optimized version tracks only two values: the current number of occupied chairs and the maximum observed occupancy. Traverse the event string once. Increment the current count for each entry event and decrement it for each exit event. After every increment, compare the value with the stored maximum and update if needed. This eliminates extra bookkeeping and keeps the logic tight: a single pass, constant memory, and only two integer updates per step. It works because the peak occupancy during the traversal directly determines the minimum chairs required.
Recommended for interviews: The incremental update approach is what most interviewers expect. It shows that you recognize the problem as a running maximum of concurrent users rather than a complex simulation. Implementing the direct simulation first demonstrates clear understanding, but the optimized version highlights strong problem decomposition and familiarity with linear scans in simulation and string processing tasks.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulate Each Event Using Two Counters | O(n) | O(1) | Best when implementing the problem exactly as described for clarity or learning simulation patterns |
| Optimized Incremental Update | O(n) | O(1) | Preferred approach in interviews; minimal logic and directly tracks peak occupancy |