Minimum Number of Chairs in a Waiting Room - Problem
You are given a string s. Simulate events at each second i:
- If
s[i] == 'E', a person enters the waiting room and takes one of the chairs in it. - If
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.
Input & Output
Example 1 — Basic Mixed Events
$
Input:
s = "EELELL"
›
Output:
2
💡 Note:
Events: Enter, Enter (2 chairs), Leave (1 chair), Enter (2 chairs), Leave, Leave (0 chairs). Maximum chairs needed: 2
Example 2 — Only Entries
$
Input:
s = "EEE"
›
Output:
3
💡 Note:
Three people enter consecutively with no one leaving. Need 3 chairs total.
Example 3 — Alternating Pattern
$
Input:
s = "ELEL"
›
Output:
1
💡 Note:
Enter (1 chair), Leave (0 chairs), Enter (1 chair), Leave (0 chairs). Only 1 chair needed maximum.
Constraints
- 1 ≤ s.length ≤ 50
- s consists only of uppercase English letters 'E' and 'L'
- s represents a valid sequence of events
Visualization
Tap to expand
Understanding the Visualization
1
Input Events
String of E (enter) and L (leave) events
2
Track Occupancy
Maintain current count and maximum seen
3
Peak Value
Return the maximum occupancy encountered
Key Takeaway
🎯 Key Insight: The minimum chairs needed equals the maximum number of people present simultaneously
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code