Ordered Set + Min-Heap | TC: O(nlogk) SC: O(k)
Approach\nUse an ordered set to maintain a sorted collection of available servers. Use a heap to store the busy servers. Use a hashmap to store the number of re
# Approach\nUse an ordered set to maintain a sorted collection of available servers. Use a heap to store the busy servers. Use a hashmap to store the number of requests handled by each server.\n\n# Complexity\n- Time complexity:\nO(nlogk)\n\n- Space complexity:\nO(k)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n        vector<pair<int, int>> busyServers; // {server id, time available}\n        set<int> availableServers;\n        unordered_map<int, int> requestsHandled;    // server id: requests\n        for (int serverId = 0; serverId < k; serverId++) {\n            availableServers.insert(serverId);\n            requestsHandled[serverId] = 0;\n        }\n\n        for (int i = 0; i < arrival.size(); i++) {\n            int timestamp = arrival[i];\n            int executionTime = load[i];\n            while (!busyServers.empty() && busyServers.front().second <= timestamp) {\n                availableServers.insert(busyServers.front().first);\n                pop_heap(busyServers.begin(), busyServers.end(), [](const auto& server1, const auto& server2) {\n                    return server1.second > server2.second;\n                });\n                busyServers.pop_back();\n            }\n\n            if (!availableServers.empty()) {\n                auto server = availableServers.lower_bound(i % k);\n                if (server == availableServers.end()) {\n                    server = availableServers.begin();\n                }\n\n                busyServers.push_back({*server, timestamp + executionTime});\n                push_heap(busyServers.begin(), busyServers.end(), [](const auto& server1, const auto& server2) {\n                    return server1.second > server2.second;\n                });\n                requestsHandled[*server]++;\n                availableServers.erase(server);\n            }\n        }\n\n        vector<int> busiestServers;\n        for (int serverId = 0; serverId < k; serverId++) {\n            if (busiestServers.empty() || requestsHandled[busiestServers.front()] < requestsHandled[serverId]) {\n                busiestServers.clear();\n                busiestServers.push_back(serverId);\n            } else if (requestsHandled[busiestServers.front()] == requestsHandled[serverId]) {\n                busiestServers.push_back(serverId);\n            }\n        }\n\n        return busiestServers;\n    }\n};\n```
['C++']