Minimum Number of Chairs in a Waiting Room - Problem

Imagine you're managing a busy waiting room where people constantly enter and leave throughout the day. Your goal is to determine the minimum number of chairs needed so that every person who enters always finds an available seat.

You are given a string s representing events that happen second by second:

  • 'E' - A person enters the waiting room and needs a chair
  • 'L' - A person leaves the waiting room, freeing up a chair

The waiting room starts completely empty. You need to find the minimum number of chairs required to ensure no one ever has to stand.

Example: If events are "EELLEL", you need 2 chairs because at most 2 people are present simultaneously.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "EELLEL"
โ€บ Output: 2
๐Ÿ’ก Note: At second 1, we have 2 people in the room (after EE), which is the maximum occupancy. We need 2 chairs to handle this peak.
example_2.py โ€” Sequential Events
$ Input: s = "ELELEL"
โ€บ Output: 1
๐Ÿ’ก Note: People enter and leave alternately, so at most 1 person is present at any time. Only 1 chair is needed.
example_3.py โ€” All Enters First
$ Input: s = "EEELLL"
โ€บ Output: 3
๐Ÿ’ก Note: All 3 people enter first (EEE), creating maximum occupancy of 3, then all leave. We need 3 chairs for the peak period.

Constraints

  • 1 โ‰ค s.length โ‰ค 50
  • s consists only of characters 'E' and 'L'
  • The number of 'L' events will never exceed 'E' events at any point (no one can leave an empty room)

Visualization

Tap to expand
Waiting Room Simulation: "EELLEL"Waiting RoomChair 1Chair 2Chair 3Timeline Processing:Et=0Count: 1Et=1Count: 2 โ˜…Lt=2Count: 1Maximum Occupancy: 2 chairs neededโ˜… Peak occurs at t=1 after two people enter
Understanding the Visualization
1
Track Current Occupancy
Keep a running count of people currently in the waiting room
2
Monitor Peak Usage
Record the highest number of simultaneous occupants
3
Determine Chair Requirement
The peak occupancy tells us exactly how many chairs we need
Key Takeaway
๐ŸŽฏ Key Insight: The maximum number of people present simultaneously determines the minimum chairs needed - no need to track individual people, just the running count!
Asked in
Google 25 Amazon 18 Apple 12 Microsoft 15
23.6K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen