This is one of those problems where the clean idea matters more than the amount of code. For 1606. Find Servers That Handled Most Number of Requests, the solution in this repository is mainly a heap / priority queue solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: heap / priority queue.
The notes already sitting in the source point us in the right direction:
- TLE
- 105 / 108 test cases passed.
- time: O(nk), space: O(k)
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are busiestServers, servers, counts.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The queue gives the solution a level-by-level or frontier-style traversal.
- The heap keeps the best candidate available without sorting the whole world every time.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(nlogk), space: O(k)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//105 / 108 test cases passed.
03//time: O(nk), space: O(k)
04class Solution {
05public:
06 vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {
07 vector<int> servers(k);
08 vector<int> counts(k);
09
10 int n = arrival.size();
11 int min_sid = 0;
12 for(int i = 0; i < n; ++i){
13 if(arrival[i] < servers[min_sid]) continue;
14 for(int j = 0; j < k; ++j){
15 int sid = (i%k+j)%k;
16 if(servers[sid] <= arrival[i]){
17 servers[sid] = arrival[i] + load[i];
18 ++counts[sid];
19 min_sid = min_element(servers.begin(), servers.end()) - servers.begin();
20 break;
21 }
22 }
23 }
24
25 int max_count = *max_element(counts.begin(), counts.end());
26 vector<int> ans;
27
28 for(int i = 0; i < counts.size(); ++i){
29 if(counts[i] == max_count){
30 ans.push_back(i);
31 }
32 }
33
34 return ans;
35 }
36};
37
38//use set and priority_queue to finding available servers
39//https://leetcode.com/problems/find-servers-that-handled-most-number-of-requests/discuss/876793/Java-O(nlogn)-use-both-TreeSet-and-PriorityQueue
40//Runtime: 1016 ms, faster than 50.00% of C++ online submissions for Find Servers That Handled Most Number of Requests.
41//Memory Usage: 118.3 MB, less than 25.00% of C++ online submissions for Find Servers That Handled Most Number of Requests.
42//time: O(nlogk), space: O(k)
43class Solution {
44public:
45 vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {
46 vector<int> counts(k);
47 set<int> availables;
48
49 for(int i = 0; i < k; ++i){
50 availables.insert(i);
51 }
52
53 //(end time, server id)
54 //the smaller end time, the earlier popped
55 auto comp = [](pair<int,int>& p1, pair<int,int>& p2){
56 return p1.first > p2.first;};
57 priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(comp)> busys(comp);
58
59 int n = arrival.size();
60 int min_sid = 0;
61 for(int i = 0; i < n; ++i){
62 int start = arrival[i];
63 int end = arrival[i] + load[i];
64 while(!busys.empty() && start >= busys.top().first){
65 pair<int,int> p = busys.top(); busys.pop();
66 availables.insert(p.second);
67 }
68 if(availables.empty()) continue;
69 //find >=
70 auto it = availables.lower_bound(i%k);
71 int sid;
72 if(it == availables.end()){
73 sid = *availables.begin();
74 }else{
75 sid = *it;
76 }
77 busys.push({end, sid});
78 ++counts[sid];
79 availables.erase(sid);
80 }
81
82 int max_count = *max_element(counts.begin(), counts.end());
83 vector<int> ans;
84
85 for(int i = 0; i < counts.size(); ++i){
86 if(counts[i] == max_count){
87 ans.push_back(i);
88 }
89 }
90
91 return ans;
92 }
93};
Cost