Memory 37.15 MB Beats 97.44% [EXPLAINED]
Intuition\nThe problem revolves around managing multiple servers that handle incoming requests based on availability. Each request can only be processed by one 
# Intuition\nThe problem revolves around managing multiple servers that handle incoming requests based on availability. Each request can only be processed by one server at a time, and if all servers are busy, the request is dropped. The goal is to find which server handled the most requests after processing all incoming requests.\n\n# Approach\nWe maintain a list of free servers and a priority queue for busy servers; as each request arrives, we check for any servers that have become free, attempt to assign the request to the preferred server (based on the request index), and if that server is busy, we look for the next available server in a circular manner.\n\n# Complexity\n- Time complexity:\nO(n log k), where n is the number of requests and k is the number of servers, due to sorting and heap operations.\n\n- Space complexity:\nO(k) for storing the count of handled requests and the list of busy servers.\n\n\n# Code\n```python3 []\nclass Solution:\n    def busiestServers(self, k: int, arrival: list[int], load: list[int]) -> list[int]:\n        import heapq\n        from sortedcontainers import SortedList\n\n        free_servers = SortedList(range(k))  # List of available servers\n        busy_servers = []  # Min-heap for busy servers\n        handled_requests = [0] * k  # Count of handled requests per server\n\n        for i in range(len(arrival)):\n            # Free up servers that have completed their tasks by the current arrival time\n            while busy_servers and busy_servers[0][0] <= arrival[i]:\n                _, server_index = heapq.heappop(busy_servers)\n                free_servers.add(server_index)  # Add back to available servers\n\n            # Determine the server to handle the current request\n            server_index = i % k\n            \n            # If the preferred server is busy, find the next available one\n            if server_index not in free_servers:\n                # Find the next available server in a circular manner\n                idx = free_servers.bisect_right(server_index)  # Get index of the next available server\n                if idx == len(free_servers):  # If no servers available after the preferred one\n                    if not free_servers:  # Check if there are any free servers at all\n                        continue  # No server available, drop the request\n                    server_index = free_servers[0]  # Wrap around to the first available server\n                else:\n                    server_index = free_servers[idx]  # Assign the next available server\n\n            # If we found a valid server\n            if server_index in free_servers:\n                # Mark the server as busy until the request is complete\n                finish_time = arrival[i] + load[i]\n                heapq.heappush(busy_servers, (finish_time, server_index))\n                handled_requests[server_index] += 1\n                free_servers.remove(server_index)  # Remove from free servers\n\n        # Find the maximum number of handled requests\n        max_requests = max(handled_requests)\n        # Return the list of servers that handled the max number of requests\n        return [i for i in range(k) if handled_requests[i] == max_requests]\n\n```
['Python3']