Leetcode 3168: Minimum Number of Chairs in a Waiting Room

grid47
grid47
Exploring patterns and algorithms
Dec 26, 2023 5 min read

You are given a string s representing a sequence of events where each character is either ‘E’ (a person enters the waiting room) or ‘L’ (a person leaves the waiting room). The waiting room starts empty. Simulate the events and determine the minimum number of chairs required to ensure there is always a chair available for every person who enters the room.
Problem
Approach
Steps
Complexity
Input: The input consists of a string `s` containing the characters 'E' and 'L'.
Example: Example 1: Input: s = "LLLEEEE" Output: 3
Constraints:
• 1 <= s.length <= 50
• s consists only of the characters 'E' and 'L'.
• s represents a valid sequence of entries and exits.
Output: Return the minimum number of chairs required to accommodate all the people entering the waiting room.
Example: Example 1: Input: s = "LLLEEEE" Output: 3
Constraints:
• The output will be an integer.
Goal: The goal is to track the number of people in the waiting room at any given time and determine the maximum number of chairs required at any point.
Steps:
• Initialize a counter to track the number of people in the waiting room.
• Iterate over the events in the string `s`.
• For each 'E', increment the counter (a person enters).
• For each 'L', decrement the counter (a person leaves).
• Keep track of the maximum value of the counter during the iteration. This represents the maximum number of people in the room at once, which is the minimum number of chairs needed.
Goal: The input string must meet the following constraints.
Steps:
• 1 <= s.length <= 50
• s consists only of 'E' and 'L'.
• The sequence of events is valid, with no more people leaving than those who have entered.
Assumptions:
• The sequence of entries and exits is valid (no 'L' without an 'E' before it).
• The waiting room starts with 0 people and is initially empty.
Input: Example 1:
Explanation: For `s = "LLLEEEE"`, at the start no one is in the room, then 3 people leave and 4 people enter, so the maximum number of people in the room at any time is 3. Therefore, the minimum number of chairs required is 3.

Input: Example 2:
Explanation: For `s = "ELELEEL"`, after each entry and exit, the number of people in the room fluctuates between 1 and 2. The maximum number of people in the room at any time is 2, so the minimum number of chairs needed is 2.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus