"Programming Marathon algorithm Directory"
"021-Database Connection Pool" "Project Download >>>"
1 Topic Description
The web system often accesses the database frequently, and performance can be poor if each access creates a new connection. To improve performance, the architect decides to reuse the connection that has already been created. When a request is received and there are no remaining connections available in the connection pool, a new connection is created and is placed in the connection pool for subsequent requests when the request processing is complete. Now that you have a log of processing requests, please analyze how many connections the connection pool needs to create.
1.1 Input Description:
The input contains multiple sets of data, and the first row of each group of data contains a positive integer n (1≤n≤1000) that represents the number of requests. Immediately following n rows, each line contains a request ID ID (A, B, C 、......、 Z) and an operation (connect or disconnect).
1.2 Output Description:
For each set of data, the maximum number of connections to be created for the output connection pool.
1.3 Input Example:
6connectconnectconnectB disconnectC disconnect
1.4 Output Example:
2
2 ideas for solving problems
This is a simple question, see code comments.
3 Algorithm Implementation
Importjava.util.*;/** * Author: Wang Junshu * time:2016-05-13 09:57 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public class Main { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {intn = scanner.nextint (); N *=2; list<string> request =NewArraylist<> (n); for(inti =0; I < n; i++) {Request.add (Scanner.next ()); } System.out.println (maxconnection (request)); } scanner.close (); }/** * Maximum number of connections * * @param request operation, two groups (A, b), A for Connection name, table B operation content, connect or disconnect * @re Turn Maximum number of connections * / Private Static int maxconnection(list<string> request) {//Record the maximum number of connections intMax =0;//Save number of connectionsSet<string> conn =NewHashset<> (Request.size ()/2);//processing each connection request for(inti =0, j = request.size (); I < J; i + =2) {String R = request.get (i); String o = request.get (i +1);//If the connection operation is added to the connection pool if("Connect". Equals (o)) {Conn.add (R);The maximum value is updated when the size in the current connection pool is larger than the maximum value that was previously recorded if(Conn.size () > Max) {max = Conn.size (); } }//Delete connection Else if("Disconnect". Equals (o)) {conn.remove (R); } }returnMax }}
4 Test Results
5 Other information
Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.
"Programming Marathon" "021-database Connection Pool"