← 返回 jpmorgan 的题目列表Count Dropped Requests
类型:online_judge
In a server farm, server efficiency can be increased by adding more cores to process more threads.\n\nEach server has cores that process threads for client requests. Every second, either threads are added to the pool or a request comes in. This is represented by an array where positive values indicate threads added and -1 indicates an incoming request.\n\nRequests come in one at a time. Each thread can serve at most one request before being destroyed. If no threads are available when a request arrives, the request is dropped.\n\nFunction Description:\nComplete the function countDroppedRequests in the editor with the following parameters:\n- int server[n]: the chronological order of processing by the server\n\nReturns:\n- int: the number of requests that are dropped\n\nConstraints:\n- 1 ≤ n ≤ 10^5\n- 1 ≤ server[i] ≤ 10^4 or server[i] = -1 for all 0 ≤ i < n\n\nExample:\n```\nserver = [1, -1, -1, 1]\n\n1. server[0] = 1: Add 1 thread, numThreads = 1, droppedRequests = 0\n2. server[1] = -1: A request arrives and is served by the thread, numThreads = 0, droppedRequests = 0\n3. server[2] = -1: A request arrives but no threads are available, request is dropped, numThreads = 0, droppedRequests = 1\n4. server[3] = 1: Add 1 thread, numThreads = 1, droppedRequests = 1\n\nReturn 1, the number of dropped requests.
Example
Input
[1, -1, -1, 1]
Output
1