只需要两个变量即可,一个维护着连接池的当前连接数,一个维护着连接池的最大连接数。
AC代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author CC11001100
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
int n = sc.nextInt();
sc.nextLine();
List<String> operators = new ArrayList<>();
while (n-- > 0) {
operators.add(sc.nextLine());
}
System.out.println(resolve(operators));
}
}
private static int resolve(List<String> operators) {
int maxConnection = 0;
int nowConnection = 0;
for (String operator : operators) {
if (operator.contains("disconnect")) {
nowConnection++;
} else if (operator.contains("connect")) {
if (nowConnection <= 0) {
maxConnection++;
} else {
nowConnection--;
}
}
}
return maxConnection;
}
}