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 in Waiting Room INPUT String s = "EELELL" E i=0 E i=1 L i=2 E i=3 L i=4 L i=5 E = Enter (need chair) L = Leave (free chair) Waiting Room Chair 1 Chair 2 Initial: 0 chairs occupied ALGORITHM STEPS 1 Initialize count=0, maxChairs=0 2 Iterate string For each char in s 3 Update count E: count++, L: count-- 4 Track maximum maxChairs = max(max, count) Execution Trace: char count max E 1 1 E 2 2 L 1 2 E 2 2 L 1 2 L 0 2 FINAL RESULT Minimum Chairs Needed: 2 Output: 2 Why 2 chairs? At index 1 (second E), 2 people are present simultaneously. Peak Occupancy: Person 1 Person 2 OK Key Insight: Track current occupancy with a counter (++ for Enter, -- for Leave). The maximum value reached during iteration equals minimum chairs needed. Time: O(n), Space: O(1). TutorialsPoint - Minimum Number of Chairs in a Waiting Room | Single Pass with Counter
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