#1606
Find Servers That Handled Most Number of Requests
HardApproaches
💡
Intuition
Time O(n²)Space O(1)
In this approach, we simply iterate through each request and check each server to see if it can handle the request. If a server is busy, we check the next server until we find one that is available or exhaust all options.
⚙️
Algorithm
3 steps- 1Step 1: Initialize an array to keep track of the end times for each server.
- 2Step 2: For each request, check if the (i % k) th server is available. If not, check the next servers in a circular manner.
- 3Step 3: If a server is found to be available, update its end time based on the current request's load and increment the count of handled requests for that server.
solution.py11 lines
1def busiestServers(k, arrival, load):
2 end_times = [0] * k
3 count = [0] * k
4 for i in range(len(arrival)):
5 while i < len(arrival) and end_times[i % k] > arrival[i]:
6 i += 1
7 if i < len(arrival):
8 end_times[i % k] = arrival[i] + load[i]
9 count[i % k] += 1
10 max_requests = max(count)
11 return [i for i in range(k) if count[i] == max_requests]ℹ
Complexity note: This complexity arises because for each request, we may need to check all servers in the worst case, leading to a quadratic time complexity.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.