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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code