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

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

转载于:https://www.cnblogs.com/cc11001100/p/8035558.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值