# 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: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
**i**^{th }character is**‘E’**, then increase the count by**1**, as more chairs are required. - If the i
^{th}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++

`// 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 time` `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 = 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 Code` `int` `main()` `{` ` ` `// Input` ` ` `// Given String` ` ` `string s = ` `"EELEE"` `;` ` ` `// Function call to find the` ` ` `// minimum number of chairs` ` ` `cout << findMinimumChairs(s);` `}` |

## Java

`// Java program for the above approach` `import` `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.` |

## Python3

`# Python 3 implementation of` `# the above approach` `import` `sys` `# Function to find the minimum number` `# of chairs required to ensure that` `# every worker is seated at any time` `def` `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 Code` `if` `__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#

`// C# program to implement` `// the above approach` `using` `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.` |

## Javascript

`<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 time` `function` `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>` |

**Output:**

3

**Time Complexity:** O(N)**Auxiliary Space:** O(1)