3168. Minimum Number of Chairs in a Waiting Room
3168. Minimum Number of Chairs in a Waiting Room
Description
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 .
Example 1:
1 | Input: s = "EEEEEEE" |
Example 2:
1 | Input: s = "ELELEEL" |
| 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:
1 | Input: s = "ELEELEELLL" |
| 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 <= 50sconsists only of the letters'E'and'L'.srepresents a valid sequence of entries and exits.
Hints/Notes
- Weekly Contest 400
Solution
Language: C++
1 | class Solution { |