📜  确保每个工人随时就座所需的最少椅子数量

📅  最后修改于: 2021-09-06 06:50:59             🧑  作者: Mango

给定一个字符串S代表工人进出休息区的记录,其中E代表进入, L代表离开休息区。每个工人需要一把椅子。任务是找到所需的最少椅子数量,以便在任何给定时间都不会缺少椅子。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个变量,比如count
  • 使用变量迭代字符串的字符,比如i
  • 如果第i字符是‘E’ ,则将计数增加1 ,因为需要更多的椅子。
  • 如果第 i字符是‘L’ ,则在其中一把椅子被清空时将计数减1
  • 打印在任何步骤获得的计数最大值。

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
#include 
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


输出:
3

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live