Input: s ="EEEEEEE"Output: 7Explanation:
After each second, a person enters the waiting room and no person leaves it.Therefore, a minimum of 7 chairs is needed.
Input: s ="ELELEEL"Output: 2Explanation:
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|11| Leave |0|22| Enter |1|13| Leave |0|24| Enter |1|15| Enter |2|06| Leave |1|1
Input: s ="ELEELEELLL"Output: 3Explanation:
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|21| Leave |0|32| Enter |1|23| Enter |2|14| Leave |1|25| Enter |2|16| Enter |3|07| Leave |2|18| Leave |1|29| Leave |0|3
We simulate the process by tracking the number of people in the room at each event. The maximum number of people present at any time is the minimum number of chairs needed.
classSolution {
public:int minimumChairs(string s) {
int curr =0, ans =0;
for (char c : s) {
if (c =='E') curr++;
else curr--;
ans = max(ans, curr);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funcminimumChairs(sstring) int {
curr, ans:=0, 0for_, c:=ranges {
ifc=='E' {
curr++ } else {
curr-- }
ifcurr > ans {
ans = curr }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
publicintminimumChairs(String s) {
int curr = 0, ans = 0;
for (char c : s.toCharArray()) {
if (c =='E') curr++;
else curr--;
ans = Math.max(ans, curr);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
funminimumChairs(s: String): Int {
var curr = 0var ans = 0for (c in s) {
if (c =='E') curr++else curr-- ans = maxOf(ans, curr)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defminimumChairs(self, s: str) -> int:
curr: int =0 ans: int =0for c in s:
if c =='E':
curr +=1else:
curr -=1 ans = max(ans, curr)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnminimum_chairs(s: String) -> i32 {
letmut curr =0;
letmut ans =0;
for c in s.chars() {
if c =='E' {
curr +=1;
} else {
curr -=1;
}
ans = ans.max(curr);
}
ans
}
}