1606. Find Servers That Handled Most Number of Requests.cpp
Code\n\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n        vector<pair<int,int>> serverLoad(k)
**Code**\n```\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n        vector<pair<int,int>> serverLoad(k);\n        std::set<int> freeServers;\n        std::priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> busyServers;\n        for (int i=0; i<k; i++) {\n            freeServers.insert(i);\n        }\n        for (int i=0; i<k; i++) {\n            serverLoad[i] = {0,i};\n        }\n        for (int i=0; i<arrival.size(); i++) {\n            while((!busyServers.empty()) && (busyServers.top().first <= arrival[i])) {\n                int server = busyServers.top().second;\n                busyServers.pop();\n                freeServers.insert(server);\n            }\n            auto itr = freeServers.lower_bound(i%k);\n            if (itr == freeServers.end()) {\n                itr = freeServers.begin();\n            }\n            if (itr != freeServers.end()) {\n                busyServers.push({arrival[i]+load[i], *itr});\n                serverLoad[*itr].first++;\n                freeServers.erase(itr);\n            }\n        }\n        sort(serverLoad.begin(), serverLoad.end(), greater<pair<int,int>>());\n        vector<int> result;\n        result.emplace_back(serverLoad[0].second);\n        for(int i=1; i<serverLoad.size(); i++) {\n            if (serverLoad[i].first == serverLoad[0].first) {\n                result.emplace_back(serverLoad[i].second);\n            }\n        }\n        return result;\n    }\n};\n```
['C']