Request Redirection
Updated
Problem statement
There are n servers in a row, numbered 1 to n. Server i has requests[i] requests to handle, but it may handle at most max_req[i] requests.
To balance the load, you can move requests from one server to another. Moving one request from server i to server j has a latency of |i - j|, the distance between the two servers. The max_latency is the largest latency among all the requests you move. If you move nothing, it is 0.
Move requests so that no server ends up with more than max_req[i] requests, and every request is still handled. Return the smallest max_latency you can reach. If there is no way to handle all the requests, return -1.
For example, with requests = [1, 3, 2, 4] and max_req = [2, 1, 5, 3], you can move 2 requests from server 2 to server 3 and 1 request from server 4 to server 3. The largest latency is 1.
Function
getMinLatency(requests: int[], max_req: int[]) → intExamples
Example 1
requests = [1, 3, 2, 4]max_req = [2, 1, 5, 3]return = 1Server 2 has 3 requests but can handle only 1, so 2 requests must move. Server 4 has 4 requests but can handle only 3, so 1 request must move.
Server 3 has room for 3 more requests. Move the 2 requests from server 2 to server 3 (latency |2 - 3| = 1) and the 1 request from server 4 to server 3 (latency |4 - 3| = 1). The largest latency is 1.
Latency 0 is impossible because some requests must move, so the answer is 1.
Constraints
1 ≤ n ≤ 10^5, andrequestsandmax_reqboth havenitems0 ≤ requests[i] ≤ 10^90 ≤ max_req[i] ≤ 10^9
Similar questions at other companies
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Data ReorganizationGoldman Sachs · OAGoldman Sachs interview questions
- Find Minimum Possible DiameterGoogle · OAGoogle interview questions
- Find Minimum TimeRippling · OARippling interview questions
- Find Optimal LevelAmazon · OA
- Get Min OperationsCitadel · OACitadel interview questions
- Minimum Days to Target EngagementTikTok · OATikTok interview questions
- Job ExecutionSnowflake · OASnowflake interview questions
- Maximizing Difference in Array Elements with Moves 🐹Microsoft · OAMicrosoft interview questions