8000 Add 3168_minimum_number_of_chairs_in_a_waiting_room · m3xw3ll/LeetCode@f22e0e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f22e0e3

Browse files
committed
Add 3168_minimum_number_of_chairs_in_a_waiting_room
1 parent aece34c commit f22e0e3

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [Minimum Number of Chairs in a Waiting Room](https://leetcode.com/problems/minimum-number-of-chairs-in-a-waiting-room/description/)
2+
3+
You are given a string s. Simulate events at each second i:
4+
5+
- If s[i] == 'E', a person enters the waiting room and takes one of the chairs in it.
6+
- If s[i] == 'L', a person leaves the waiting room, freeing up a chair.
7+
8+
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.
9+
10+
Example 1:
11+
```
12+
Input: s = "EEEEEEE"
13+
14+
Output: 7
15+
16+
Explanation:
17+
18+
After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.
19+
```
20+
Example 2:
21+
```
22+
Input: s = "ELELEEL"
23+
24+
Output: 2
25+
26+
Explanation:
27+
28+
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.
29+
30+
Second Event People in the Waiting Room Available Chairs
31+
0 Enter 1 1
32+
1 Leave 0 2
33+
2 Enter 1 1
34+
3 Leave 0 2
35+
4 Enter 1 1
36+
5 Enter 2 0
37+
6 Leave 1 1
38+
```
39+
Example 3:
40+
```
41+
Input: s = "ELEELEELLL"
42+
43+
Output: 3
44+
45+
Explanation:
46+
47+
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.
48+
49+
Second Event People in the Waiting Room Available Chairs
50+
0 Enter 1 2
51+
1 Leave 0 3
52+
2 Enter 1 2
53+
3 Enter 2 1
54+
4 Leave 1 2
55+
5 Enter 2 1
56+
6 Enter 3 0
57+
7 Leave 2 1
58+
8 Leave 1 2
59+
9 Leave 0 3
60+
```
61+
Solution
62+
```python
63+
class Solution:
64+
def minimumChairs(self, s: str) -> int:
65+
num_people = 0
66+
max_people = 0
67+
for i in s:
68+
if i == 'E':
69+
num_people += 1
70+
else:
71+
num_people -= 1
72+
max_people = max(max_people, num_people)
73+
return max_people
74+
75+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def minimum_chairs(s):
2+
num_people = 0
3+
max_people = 0
4+
for i in s:
5+
if i == 'E':
6+
num_people += 1
7+
else:
8+
num_people -= 1
9+
max_people = max(max_people, num_people)
10+
return max_people
11+
12+
print(minimum_chairs("ELELEEL"))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ To search for a specific problem please use ```STRG + F``` to search for.
768768
| 3151 | [Special Array I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3151_special_array_one.md)
769769
| 3158 | [Find the XOR of Numbers Which Appear Twice](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3158_find_the_xor_of_numbers_which_appear_twice.md)
770770
| 3162 | [Find the Number of Good Pairs I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3162_find_the_number_of_good_pairs_one.md)
771+
| 3168 | [Minimum Number of Chairs in a Waiting Room](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3168_minimum_number_of_chairs_in_a_waiting_room.md)
771772
| 3174 | [Clear Digits](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3174_clear_digits.md)
772773
| 3184 | [Count Pairs That Form a Complete Day I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3184_count_pairs_that_form_a_complete_day_one.md)
773774
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3190_find_minimum_operations_to_make_all_elements_divisible_by_three.md)

0 commit comments

Comments
 (0)
0