You are given a string s. Simulate events at each second i:
s[i] == 'E', a person enters the waiting room and takes one of the chairs in it.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.
Example 1:
Input: s = "EEEEEEE"
Output: 7
Explanation:
After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.
Example 2:
Input: s = "ELELEEL"
Output: 2
Explanation:
Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.
| Second | Event | People in the Waiting Room | Available Chairs |
|---|---|---|---|
| 0 | Enter | 1 | 1 |
| 1 | Leave | 0 | 2 |
| 2 | Enter | 1 | 1 |
| 3 | Leave | 0 | 2 |
| 4 | Enter | 1 | 1 |
| 5 | Enter | 2 | 0 |
| 6 | Leave | 1 | 1 |
Example 3:
Input: s = "ELEELEELLL"
Output: 3
Explanation:
Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.
| Second | Event | People in the Waiting Room | Available Chairs |
|---|---|---|---|
| 0 | Enter | 1 | 2 |
| 1 | Leave | 0 | 3 |
| 2 | Enter | 1 | 2 |
| 3 | Enter | 2 | 1 |
| 4 | Leave | 1 | 2 |
| 5 | Enter | 2 | 1 |
| 6 | Enter | 3 | 0 |
| 7 | Leave | 2 | 1 |
| 8 | Leave | 1 | 2 |
| 9 | Leave | 0 | 3 |
Constraints:
1 <= s.length <= 50s consists only of the letters 'E' and 'L'.s represents a valid sequence of entries and exits.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to figure out the fewest chairs needed in a waiting room given arrival and departure times. The brute force method means we'll try out every possible number of chairs to see if it works.
Here's how the algorithm would work step-by-step:
def minimum_number_of_chairs_brute_force(arrival_times, departure_times):
number_of_people = len(arrival_times)
number_of_chairs = 0
while True:
number_of_chairs += 1
chairs_available = [True] * number_of_chairs
chair_usage_times = [[] for _ in range(number_of_chairs)]
can_accommodate_all = True
for person_index in range(number_of_people):
arrival_time = arrival_times[person_index]
departure_time = departure_times[person_index]
chair_found = False
for chair_index in range(number_of_chairs):
# Iterate to find an available chair
is_chair_free = True
for occupied_time in chair_usage_times[chair_index]:
if not (departure_time <= occupied_time[0] or arrival_time >= occupied_time[1]):
is_chair_free = False
break
if is_chair_free:
# Mark the chair as used during this time
chair_usage_times[chair_index].append((arrival_time, departure_time))
chair_found = True
break
if not chair_found:
can_accommodate_all = False
break
# If we can accomodate all, return number of chairs
if can_accommodate_all:
return number_of_chairsTo find the minimum number of chairs needed, we focus on when people arrive and leave. We'll sort the arrival and departure times, and then walk through them like a timeline, tracking how many people are in the room at any given moment.
Here's how the algorithm would work step-by-step:
def min_chairs_needed(arrival_times, departure_times):
events = []
for arrival_time in arrival_times:
events.append((arrival_time, 'arrival'))
for departure_time in departure_times:
events.append((departure_time, 'departure'))
# Sort events by time, arrivals prioritized
events.sort(key=lambda x: (x[0], x[1] == 'departure'))
chairs_needed_at_the_same_time = 0
max_chairs_needed = 0
for _, event_type in events:
# Arrival means we need an extra chair
if event_type == 'arrival':
chairs_needed_at_the_same_time += 1
max_chairs_needed = max(max_chairs_needed, chairs_needed_at_the_same_time)
# Departure frees up a chair
else:
chairs_needed_at_the_same_time -= 1
return max_chairs_needed| Case | How to Handle |
|---|---|
| Null or empty arrival and departure arrays | Return 0 if both arrays are null or empty, as no chairs are needed. |
| Arrival and departure arrays have different lengths | Throw an IllegalArgumentException or return -1 to indicate invalid input. |
| Single arrival and departure event | Return 1, as only one chair is needed at most. |
| Arrival and departure times are the same (arrival[i] == departure[i]) | This should be handled correctly by any valid algorithm, as the person arrives and immediately leaves, not affecting peak occupancy. |
| Arrival and departure times are out of order (departure[i] < arrival[i]) | Throw an IllegalArgumentException or correct the order before processing. |
| Arrival and departure times contain large numbers (potential integer overflow) | Use long instead of int to store times to prevent potential integer overflow. |
| All individuals arrive at the same time, and then depart at different times | The maximum number of concurrent individuals will be the total number of individuals, and the solution should correctly count this. |
| Individuals arrive at different times, and all depart at the same time | Only one chair is needed as only one person is present at any given moment. |