TreeSet + Map + Heap solution
Intuition\nThis problem is similar to https://leetcode.com/problems/meeting-rooms-iii/description/ where we also need to schedule some tasks. The key difference
# Intuition\nThis problem is similar to https://leetcode.com/problems/meeting-rooms-iii/description/ where we also need to schedule some tasks. The key differences are:\n- In Meeting Rooms III we need to put the meeting in the next available room with smallest number (Heap!! Heap!! Heap!!).\n- We need to wait until a room becomes empty\n\nOn the contrary, this problem does not have any such limitations. Thus we can use a treeSet to store freeServers and leverage .ceiling() method to find the next available server.\n\n\n# Complexity\n- Time complexity: O(n * log(n))\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 {\n    public List<Integer> busiestServers(int k, int[] arrival, int[] load) {\n        int[][] serverTimes = buildServerTimes(arrival, load);\n\n        TreeSet<Integer> freeServers = new TreeSet<>();\n        for (int i = 0; i < k; i++) {\n            freeServers.add(i);\n        }    \n\n        // [endTime, serverNumber]\n        PriorityQueue<int[]> serverSchedules = new PriorityQueue<int[]>(new Comparator<int[]>(){\n            public int compare(int[] a, int[] b) {\n                return a[0] - b[0];\n            }\n        });\n\n        Map<Integer, Integer> serverCountMap = new HashMap<>();\n\n        for (int i = 0; i < serverTimes.length; i++) {\n            int start = serverTimes[i][0];\n            int end = serverTimes[i][1];\n\n            while (!serverSchedules.isEmpty() && serverSchedules.peek()[0] <= start) {\n                freeServers.add(serverSchedules.poll()[1]);\n            }\n\n            // Ignore the requests which can\'t be served\n            if (!freeServers.isEmpty()) {\n                int serverNumber = findNextAvailableServer(freeServers, i, k);\n                freeServers.remove(serverNumber);\n                serverSchedules.offer(new int[]{end, serverNumber});\n                int requestCount = serverCountMap.getOrDefault(serverNumber, 0) + 1;\n                serverCountMap.put(serverNumber, requestCount);\n            }\n        }\n\n        int maxCount = 0;\n        for (int requestCount : serverCountMap.values()) {\n            maxCount = Math.max(maxCount, requestCount);\n        }\n\n        List<Integer> result = new ArrayList<>();\n        for (int serverNumber : serverCountMap.keySet()) {\n            if (serverCountMap.get(serverNumber) == maxCount) {\n                result.add(serverNumber);\n            }\n        }\n\n        return result;\n    }\n\n    private int findNextAvailableServer(TreeSet<Integer> freeServers, int i, int k) {\n        Integer serverNumber = freeServers.ceiling(i % k);\n        if (serverNumber == null) {\n            return freeServers.first();\n        }\n\n        return serverNumber;\n    }\n\n    private int[][] buildServerTimes(int[] arrival, int[] load) {\n        int length = arrival.length;\n\n        int[][] serverTimes = new int[length][2];\n\n        for (int i = 0; i < length; i++) {\n            serverTimes[i] = new int[]{arrival[i], arrival[i] + load[i]};\n        }\n\n        return serverTimes;\n    }\n}\n```
['Java']