[LeetCode] Minimum Number of Chairs in a Waiting Room

3168. Minimum Number of Chairs in a Waiting Room

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int minimumChairs(string s) {
int res = 0, now = 0;
for(auto& ch : s) {
if(ch == 'E') now++;
else now--;
res = max(res, now);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/06/02/PS/LeetCode/minimum-number-of-chairs-in-a-waiting-room/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.