C++ | Greedy | Easy solution | Priority Queue
Intuition\n Describe your first thoughts on how to solve this problem. \nThis is similar to meeting rooms III question. We can use set, priority queue and map t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is similar to meeting rooms III question. We can use set, priority queue and map to solve this question. Solve this using greedy approach.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTry to break the solution in smaller parts and you will be able to solve it easily.\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n        \n        int n = arrival.size();\n        vector<int> endTime(n);\n        for(int i=0;i<n;i++)\n            endTime[i] = arrival[i] + load[i];\n\n        //(endTime,serverNo)\n\n        priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> trackRequests;\n        set<int> unusedServers;\n        unordered_map<int,int> freq;        \n\n        for(int i=0;i<k;i++)\n            unusedServers.insert(i);\n        \n        for(int i=0;i<n;i++)\n        {\n            int start = arrival[i];\n            int end = endTime[i];\n\n            // remove all the requests that has been done\n            while(!trackRequests.empty() && trackRequests.top().first<=start)\n            {\n                unusedServers.insert(trackRequests.top().second);\n                trackRequests.pop();\n            }\n\n            int assignServer = i%k;\n            if(unusedServers.find(assignServer)!=unusedServers.end())\n            {\n                unusedServers.erase(assignServer);\n                trackRequests.push({end, assignServer});\n                freq[assignServer]++;\n            }\n            else\n            {\n                if(unusedServers.size()==0)\n                {\n                    // all servers are busy\n                    continue;\n                }\n                else\n                {\n                    auto lb = unusedServers.lower_bound(assignServer);\n                    if(lb==unusedServers.end())\n                    {\n                        auto lb_start = unusedServers.lower_bound(0);\n                        int newGivenServer = *lb_start;\n                        unusedServers.erase(newGivenServer);\n                        trackRequests.push({end, newGivenServer});\n                        freq[newGivenServer]++;\n                    }\n                    else\n                    {\n                        int newGivenServer = *lb;\n                        unusedServers.erase(newGivenServer);\n                        trackRequests.push({end, newGivenServer});\n                        freq[newGivenServer]++;\n                    }\n                }\n            }\n        }\n\n        int maxValue = 0;\n        vector<int> server;\n\n        for(auto m: freq)\n            maxValue = max(maxValue, m.second);\n        \n        for(auto m: freq)\n        {\n            if(m.second == maxValue)\n                server.push_back(m.first);\n        }\n\n        return server;\n\n    }\n};\n```
['C++']