Problem Description
Given k servers (numbered 0 to k-1) and a series of requests arriving at specific times with given execution durations, assign requests to servers based on a specific assignment strategy. Each request i is ideally assigned to server (i % k). If that server is busy, assign it to the next available server (cycling back to 0 if necessary). If no server is available when a request arrives, the request is dropped. The goal is to determine which server(s) handled the most requests.
Key Insights
- Each server can process one request at a time.
- Use an ordered set (or equivalent) to quickly find the next available server with an id >= (i % k). If none exists, wrap around and take the smallest available server.
- A min-heap helps track busy servers by storing tuples of (finish time, server id).
- Upon each request, first free all servers in the busy heap that have completed by the current arrival time.
- Maintain a count array to track the number of requests served by each server.
Space and Time Complexity
Time Complexity: O(n log k), where n is the number of requests. Each request may involve log k operations when searching/updating the available servers and busy heap. Space Complexity: O(k + n) in the worst case for storing server states and request counts.
Solution
The solution involves two main data structures:
- An ordered set (or similar structure) of available server IDs. This allows quick access to the smallest available server ID not less than a given starting point. If no such server exists, it wraps around to the smallest server.
- A min-heap for busy servers, storing pairs (finish_time, server_id) to know when servers become free.
For each request:
- Free all servers from the busy heap whose finish time is less than or equal to the arrival time of the current request.
- Use the ordered set to find the server with the lowest id that is at least (i % k). If not found, choose the smallest server in the set.
- If an available server is found, update its count, remove it from the available set, and push the pair (arrival_time + load, server_id) into the busy heap.
- Finally, determine the maximum request count and return all servers with that count.