Fork me on GitHub

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

image

image

 

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

 

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

posted @ 2017-12-13 23:42  CC11001100  阅读(213)  评论(0编辑  收藏  举报