C++ | PQ + Set | With Comments
\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n        vector<int> result;\n        unordered_ma
```\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n        vector<int> result;\n        unordered_map<int, int> serverReqCount;\n        \n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq;\n        set<int> availableServers;\n        \n        //Assign the first k request to each server, add in PQ\n        for(int i = 0; i < k && i < arrival.size(); i++)\n        {\n            pq.push({arrival[i] + load[i], i});\n            serverReqCount[i]++;\n        }\n        \n        for(int i = k; i < arrival.size(); i++)\n        {\n            if(availableServers.size() || (pq.size() && arrival[i] >= pq.top().first))\n            {\n                //Check if any other server got free.\n                while(pq.size() && arrival[i] >= pq.top().first)\n                {\n                    //Add in to available list.\n                    availableServers.insert(pq.top().second);\n                    pq.pop();\n                }\n                \n                //Find the first server in order.\n                auto server = lower_bound(availableServers.begin(), availableServers.end(), i%k);\n                \n                //Roll over if no server found.\n                if(server == availableServers.end())\n                    server = availableServers.begin();\n                \n                //Update the ds.\n                serverReqCount[*server]++;\n                pq.push({arrival[i]+load[i], *server});                    \n                \n                //Remove from available list.\n                availableServers.erase(server);                    \n            }\n        }\n        \n        \n        //Get the max\n        int maxr = INT_MIN;\n        for(auto& sc: serverReqCount)\n            maxr = max(maxr, sc.second);\n        \n        \n        //There can be multiple servers with same req count.\n        for(auto& sc: serverReqCount)\n        {\n            if(sc.second == maxr)\n                result.push_back(sc.first);\n        }\n            \n        return result;\n    }\n};\n```
[]