Only two variables are required, one maintains the current number of connections for the connection pool, and one maintains the maximum number of connections for the connection pool.
AC Code:
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;}}
Title Source: https://www.nowcoder.com/practice/05f97d9b29944c018578d98d7f0ce56e?tpid=3&tqid=10884&tpage=1& Rp=&ru=/ta/hackathon&qru=/ta/hackathon/question-ranking
Programming practice of New Ket Network Marathon: Database connection pool