牛客网编程练习之编程马拉松:数据库连接池

牛客网编程练习之编程马拉松:数据库连接池

 

只需要两个变量即可,一个维护着连接池的当前连接数,一个维护着连接池的最大连接数。

 

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

相关文章:

  • 2021-09-03
  • 2021-06-26
  • 2021-10-10
  • 2021-07-25
  • 2021-04-04
  • 2021-05-16
  • 2021-05-10
  • 2022-12-23
猜你喜欢
  • 2021-07-13
  • 2021-10-10
  • 2022-12-23
  • 2021-11-17
  • 2021-05-16
  • 2021-05-30
  • 2021-06-07
相关资源
相似解决方案