Weighted Server Load Balancer with TTL
Problem statement
Assign tasks to available servers while tracking active weighted load.
- Each server row is
name,maxLoad. - Each task row is
time,taskId,weight,ttl.
Process tasks in input order; task times are nondecreasing. Before each task, expire every earlier assignment whose end time startTime + ttl is at most the current task's time, and remove its weight from that server.
A server is eligible when its current load plus the task's weight does not exceed maxLoad. Choose the eligible server with the smallest current load, breaking ties by lexicographically smaller server name. An accepted task remains active until its end time.
Return one result per task as taskId:serverName. Return taskId:NONE when no server can accept the task.
Function
assignWeightedTasks(servers: String[], tasks: String[]) → String[]Examples
Example 1
servers = ["a,5","b,4"]tasks = ["0,t1,3,5","1,t2,2,3","4,t3,4,2","5,t4,5,1"]return = ["t1:a","t2:b","t3:b","t4:a"]The first task uses lexical tie-breaking. The second goes to the lighter server. Its TTL expires at time 4, making b available for t3; t1 expires before t4.
Example 2
servers = ["solo,3"]tasks = ["0,t1,3,5","0,t2,1,2","5,t3,3,1"]return = ["t1:solo","t2:NONE","t3:solo"]The second task exceeds the remaining capacity. The first task expires at time 5, so the final task is accepted.
Example 3
servers = ["zeta,10","alpha,10"]tasks = ["2,x,1,3","2,y,1,3"]return = ["x:alpha","y:zeta"]Both servers begin at load 0, so alpha wins the lexical tie. The next task then chooses the lighter zeta.
Constraints
1 <= servers.length <= 1000; server names are unique.1 <= tasks.length <= 10000; task IDs are unique and times are nondecreasing.- Every
maxLoad, task weight, and TTL is a positive integer at most10^9. - Times and computed end times fit in signed
64-bit integers. - Rejected tasks do not consume load.
- Tasks at the same time are processed in input order, and expirations at that time happen before assignment.