Problem Description — Load Balancer
Implement a prototype round-robin load balancing algorithm for n servers, numbered from 1 to n, handling m incoming requests.
Each request i:
- arrives at time
arrival[i] - takes
burstTime[i]time units to execute
Server Assignment Rules
- When a request arrives, it is assigned to the available server with the lowest index
- Once assigned, a server becomes unavailable from
arrival[i]toarrival[i] + burstTime[i] - If multiple requests arrive at the same time, they are processed in order of their original indices
- If no server is available at the arrival time, the request is dropped and assigned
-1
Output Requirement
For each request, return:
- the 1-based index of the server that processes it
- or
-1if the request is dropped
Function Description
Complete the function:
getServerIndex(n, arrival, burstTime)
Parameters
int n
Number of serversint arrival[m]
Arrival times of requestsint burstTime[m]
Execution time for each request
Returns
int[m]
Array where each element is:- the 1-based server index
- or
-1if no server is available
Example
n = 3
arrival = [2, 4, 1, 8, 9]
burstTime = [7, 9, 2, 4, 5]
Request Processing Order (by arrival time)
| Request Index | Arrival | Burst | Completion |
|---|---|---|---|
| 3 | 1 | 2 | 3 |
| 1 | 2 | 7 | 9 |
| 2 | 4 | 9 | 13 |
| 4 | 8 | 4 | 12 |
| 5 | 9 | 5 | 14 |
Output
[2, 1, 1, 3, 2]
Constraints
1 ≤ n ≤ 10^51 ≤ m ≤ 10^51 ≤ arrival[i] ≤ 10^91 ≤ burstTime[i] ≤ 10^9
典型的系统设计 + 数据结构结合题 ,核心考察:
- 时间线模拟能力
- 可用资源管理(服务器池)
- 对「并发 + 排序规则」的理解
正文完