Minimum number of chairs required to ensure that every worker is seated at any instant - GeeksforGeeks

22 Apr, 2021
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: 3Explanation: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++
Java
Python3
C#
Javascript
// C++ implementation of// the above approach#include <bits/stdc++.h>using namespace std; // Function to find the minimum number// of chairs required to ensure that// every worker is seated at any timeint findMinimumChairs(string s){    // Stores the number of    // chairs required    int count = 0;     // Pointer to iterate    int i = 0;     // Stores minimum number of    // chairs required    int mini = INT_MIN;     // Iterate over every character    while (i < s.length()) {         // If character is 'E'        if (s[i] == 'E')             // Increase the count            count++;         // Otherwise        else            count--;         // Update maximum value of count        // obtained at any given time        mini = max(count, mini);         i++;    }     // Return mini    return mini;} // Driver Codeint main(){    // Input     // Given String    string s = "EELEE";     // Function call to find the    // minimum number of chairs    cout << findMinimumChairs(s);}
// Java program for the above approachimport java.io.*;import java.util.*; class GFG{     // Function to find the minimum number  // of chairs required to ensure that  // every worker is seated at any time  static int findMinimumChairs(String s)  {         // Stores the number of    // chairs required    int count = 0;     // Pointer to iterate    int i = 0;     // Stores minimum number of    // chairs required    int mini = Integer.MIN_VALUE;     // Iterate over every character    while (i < s.length()) {       // If character is 'E'      if (s.charAt(i) == 'E')         // Increase the count        count++;       // Otherwise      else        count--;       // Update maximum value of count      // obtained at any given time      mini = Math.max(count, mini);       i++;    }     // Return mini    return mini;  }   // Driver code  public static void main(String[] args)  {    // Input     // Given String    String s = "EELEE";     // Function call to find the    // minimum number of chairs    System.out.print(findMinimumChairs(s));  }} // This code is contributed by code_hunt.
# Python 3 implementation of# the above approachimport sys # Function to find the minimum number# of chairs required to ensure that# every worker is seated at any timedef findMinimumChairs(s):       # Stores the number of    # chairs required    count = 0     # Pointer to iterate    i = 0     # Stores minimum number of    # chairs required    mini = -sys.maxsize - 1     # Iterate over every character    while (i < len(s)):               # If character is 'E'        if (s[i] == 'E'):                       # Increase the count            count += 1         # Otherwise        else:            count -= 1         # Update maximum value of count        # obtained at any given time        mini = max(count, mini)        i += 1     # Return mini    return mini # Driver Codeif __name__ == '__main__':       # Input     # Given String    s = "EELEE"     # Function call to find the    # minimum number of chairs    print(findMinimumChairs(s))         # This code is contributed by ipg2016107.
// C# program to implement// the above approachusing System;using System.Collections.Generic; class GFG{   // Function to find the minimum number  // of chairs required to ensure that  // every worker is seated at any time  static int findMinimumChairs(string s)  {     // Stores the number of    // chairs required    int count = 0;     // Pointer to iterate    int i = 0;     // Stores minimum number of    // chairs required    int mini = Int32.MinValue;     // Iterate over every character    while (i < s.Length) {       // If character is 'E'      if (s[i] == 'E')         // Increase the count        count++;       // Otherwise      else        count--;       // Update maximum value of count      // obtained at any given time      mini = Math.Max(count, mini);       i++;    }     // Return mini    return mini;  }    // Driver code  public static void Main()  {    // Input     // Given String    string s = "EELEE";     // Function call to find the    // minimum number of chairs    Console.WriteLine(findMinimumChairs(s));  }} // This code is contributed by code_hunt.
<script> // Javascript implementation of// the above approach // Function to find the minimum number// of chairs required to ensure that// every worker is seated at any timefunction findMinimumChairs(s){    // Stores the number of    // chairs required    var count = 0;     // Pointer to iterate    var i = 0;     // Stores minimum number of    // chairs required    var mini = -2147483647;     // Iterate over every character    while (i < s.length) {         // If character is 'E'        if (s[i] == 'E')             // Increase the count            count++;         // Otherwise        else            count--;         // Update maximum value of count        // obtained at any given time        mini = Math.max(count, mini);         i++;    }     // Return mini    return mini;} // Driver Code    // Input     // Given String    var s = "EELEE";     // Function call to find the    // minimum number of chairs    document.write(findMinimumChairs(s)); </script>
  
3
 
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
ipg2016107
code_hunt
bgangwar59
counter
Mathematical
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Python program to check if a string is palindrome or not
Check for Balanced Brackets in an expression (well-formedness) using Stack