Minimum number of chairs required to ensure that every worker is seated at any instant
Given a string S representing the record of workers entering and leaving the rest area, where E represents entering and L represents leaving the rest area. For each worker, one chair is required. The task is to find the minimum number of chairs required so that there is no shortage of chairs at any given time.
Examples:
Input: S = “EELEE”
Output: 3
Explanation:
Step 1: One person enters. Therefore, 1 chair is required for now.
Step 2: Another person enters. Therefore, the number of chairs are required to be increased to 2.
Step 3: One person leaves. Therefore, no need to increase the number of chairs.
Step 4: Another person enters. Still, no need to increase the number of chairs.
Step 4: Another person enters. Therefore, number of chairs are needed to be increased to 3.
Therefore, minimum 3 chairs are required such that there is never a shortage of chairs.
Input: S = “EL”
Output: 1
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say, count.
- Iterate over the characters of the string using a variable, say i.
- If the ith character is ‘E’, then increase the count by 1, as more chairs are required.
- If the ith character is ‘L’, then decrease the count by 1 as one of the chairs is emptied.
- Print the maximum value of count obtained at any step.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumChairs(string s)
{
int count = 0;
int i = 0;
int mini = INT_MIN;
while (i < s.length()) {
if (s[i] == 'E' )
count++;
else
count--;
mini = max(count, mini);
i++;
}
return mini;
}
int main()
{
string s = "EELEE" ;
cout << findMinimumChairs(s);
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int findMinimumChairs(String s)
{
int count = 0 ;
int i = 0 ;
int mini = Integer.MIN_VALUE;
while (i < s.length()) {
if (s.charAt(i) == 'E' )
count++;
else
count--;
mini = Math.max(count, mini);
i++;
}
return mini;
}
public static void main(String[] args)
{
String s = "EELEE" ;
System.out.print(findMinimumChairs(s));
}
}
|
Python3
import sys
def findMinimumChairs(s):
count = 0
i = 0
mini = - sys.maxsize - 1
while (i < len (s)):
if (s[i] = = 'E' ):
count + = 1
else :
count - = 1
mini = max (count, mini)
i + = 1
return mini
if __name__ = = '__main__' :
s = "EELEE"
print (findMinimumChairs(s))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int findMinimumChairs( string s)
{
int count = 0;
int i = 0;
int mini = Int32.MinValue;
while (i < s.Length) {
if (s[i] == 'E' )
count++;
else
count--;
mini = Math.Max(count, mini);
i++;
}
return mini;
}
public static void Main()
{
string s = "EELEE" ;
Console.WriteLine(findMinimumChairs(s));
}
}
|
Javascript
<script>
function findMinimumChairs(s)
{
var count = 0;
var i = 0;
var mini = -2147483647;
while (i < s.length) {
if (s[i] == 'E' )
count++;
else
count--;
mini = Math.max(count, mini);
i++;
}
return mini;
}
var s = "EELEE" ;
document.write(findMinimumChairs(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
22 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...