时间:2021-07-01 10:21:17 帮助过:12人阅读
只需要两个变量即可,一个维护着连接池的当前连接数,一个维护着连接池的最大连接数。
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;
}
}
题目来源: https://www.nowcoder.com/practice/05f97d9b29944c018578d98d7f0ce56e?tpId=3&tqId=10884&tPage=1&rp=&ru=/ta/hackathon&qru=/ta/hackathon/question-ranking
牛客网编程练习之编程马拉松:数据库连接池
标签:line display rgs nta 数据库 int() 数据库连接 list cti