Connection Pool Peak
MediumSimulation · Oracle
A database connection pool has room for at most max_pool connections at once. Each request [start, end] asks for one connection from time start until time end. The requests list is sorted by start (requests with equal start are handled in list order).
When a request arrives at time start:
- Every accepted connection whose
end <= starthas already been released. (A connection ending at time 5 frees its slot for a request starting at 5.) - If fewer than
max_poolaccepted connections are still in use, the request is accepted and holds a slot until itsend. - Otherwise it is rejected and never retried.
Return [peak, rejected]: the largest number of connections in use at the same time (counting only accepted requests), and how many requests were rejected.
Example 1
- Input:
- requests = [[0, 10], [2, 6], [3, 8], [6, 9], [10, 12]], max_pool = 2
- Output:
- [2, 1]
- Explanation
[0,10]and[2,6]fill the pool, so[3,8]is rejected. At time 6 the second connection is released, so[6,9]gets in. At time 10 the first is released and[10,12]is accepted. The pool never holds more than 2, and one request was rejected.
Example 2
- Input:
- requests = [[1, 4], [2, 5], [3, 6]], max_pool = 5
- Output:
- [3, 0]
- Explanation
- All three overlap at time 3, and a pool of 5 can hold them, so the peak is 3 with no rejections.
Example 3
- Input:
- requests = [[0, 1], [1, 2]], max_pool = 0
- Output:
- [0, 2]
- Explanation
- With no slots, every request is rejected and the peak stays 0.
Constraints
- 0 ≤ requests.length ≤ 105
- 0 ≤ start < end ≤ 109
requestsis sorted bystart.- 0 ≤ max_pool ≤ 105