给定一个字符串S代表进入和离开休息区的工人的记录,其中E代表进入, L代表离开休息区。每位工人需要一把椅子。任务是找到所需的最少椅子数量,以使在任何给定时间都不会短缺椅子。
例子:
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. Theefore, 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
方法:请按照以下步骤解决问题:
- 初始化变量,例如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.
3
时间复杂度: O(N)
辅助空间: O(1)