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
Minimum Chairs Problem: Peak Occupancy TrackingInput EventsE E L E L LTrack Current Count1 → 2 → 1 → 2 → 1 → 0Maximum2Occupancy Over Time121210Answer: 2 chairs minimum needed
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
Asked in
Amazon 15 Google 12
30.3K Views
Medium Frequency
~8 min Avg. Time
890 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