调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
困难
相关标签
相关企业
提示

Design a simple load distributor for a data center that can do the following:

  • Add and remove machines from the cluster.
  • Add applications to run on a machine.
  • Stop applications that are running on a machine.
  • Return a list of the applications running on a machine.

Implement the DCLoadBalancer class:

  • DCLoadBalancer() Initializes the object.
  • void addMachine(int machineId, int capacity) Registers a machine with the given machineId and maximum capacity.
  • void removeMachine(int machineId) Removes the machine with the given machineId. All applications running on this machine are automatically reallocated to other machines in the same order as they were added to this machine. The applications should be reallocated in the same manner as addApplication.
  • int addApplication(int appId, int loadUse) Allocates an application with the given appId and loadUse to the machine with the largest remaining capacity that can handle the additional request. If there is a tie, the machine with the lowest ID is used. Returns the machine ID that the application is allocated to. If no machine can handle the request, return -1.
  • void stopApplication(int appId) Stops and removes the application with the given appId from the machine it is running on, freeing up the machine's capacity by its corresponding loadUse. If the application does not exist, nothing happens.
  • List<Integer> getApplications(int machineId) Returns a list of application IDs running on a machine with the given machineId in the order in which they were added. If there are more than 10 applications, return only the first 10 IDs.

 

Example 1:

Input
["DCLoadBalancer", "addMachine", "addMachine", "addMachine", "addMachine", "addApplication", "addApplication", "addApplication", "addApplication", "getApplications", "addMachine", "addApplication", "stopApplication", "addApplication", "getApplications", "removeMachine", "getApplications"]
[[], [1, 1], [2, 10], [3, 10], [4, 15], [1, 3], [2, 11], [3, 6], [4, 5], [2], [5, 10], [5, 5], [3], [6, 5], [4], [4], [2]]
Output
[null, null, null, null, null, 4, 4, 2, 3, [3], null, 5, null, 2, [1, 2], null, [6, 1]]

Explanation
DCLoadBalancer dCLoadBalancer = new DCLoadBalancer();
dCLoadBalancer.addMachine(1, 1); // Capacity Left: [1]
dCLoadBalancer.addMachine(2, 10); // Capacity Left: [1,10]
dCLoadBalancer.addMachine(3, 10); // Capacity Left: [1,10,10]
dCLoadBalancer.addMachine(4, 15); // Capacity Left: [1,10,10,15]
dCLoadBalancer.addApplication(1, 3); // return 4, Capacity Left: [1,10,10,12]
                                     // Machine 4 had the largest capacity left at 15.
dCLoadBalancer.addApplication(2, 11); // return 4, Capacity Left: [1,10,10,1]
                                      // Machine 4 had the largest capacity left at 12.
dCLoadBalancer.addApplication(3, 6); // return 2, Capacity Left: [1,4,10,1]
                                     // Machine 2 and 3 had the same largest capacity but machine 2 has the lower ID.
dCLoadBalancer.addApplication(4, 5); // return 3, Capacity Left: [1,4,5,1]
                                     // Machine 3 had the largest capacity at 10.
dCLoadBalancer.getApplications(2); // return [3], Machine 2 only has application 3.
dCLoadBalancer.addMachine(5, 10); // Capacity Left: [1,4,5,1,10]
dCLoadBalancer.addApplication(5, 5); // return 5, Capacity Left: [1,4,5,1,5]
                                     // Machine 5 had the largest capacity at 10.
dCLoadBalancer.stopApplication(3); // Capacity Left: [1,10,5,1,5], 
                                   // Application 3 was running on machine 2.
dCLoadBalancer.addApplication(6, 5); // return 2, Capacity Left: [1,5,5,1,5]
                                     // Machine 2 had the largest capacity at 10.
dCLoadBalancer.getApplications(4); // return [1, 2], Machine 4 has applications 1 and 2.
dCLoadBalancer.removeMachine(4); // Capacity Left: [1,2,5,-,5]
                                 // Machine 4 had applications 1 and 2.
                                 // Application 1 has a load of 3 and is added to machine 2.
                                 // Application 2 has a load of 11 and cannot be added to any machine so it is removed.
dCLoadBalancer.getApplications(2); // return [6, 1], Machine 2 has applications 6 and 1.

 

Constraints:

  • 1 <= machineId, appId <= 105
  • 1 <= loadUse, capacity <= 105
  • machineId will not have the same ID as any active machine across all calls to addMachine.
  • machineId will be the ID of an active machine across all calls to removeMachine and getApplications.
  • appId will be unique across all calls to addApplication.
  • At most 10 calls will be made to removeMachine.
  • At most 2 * 104 calls in total will be made to addMachine, removeMachine, addApplication, stopApplication and getApplications.
通过次数
7
提交次数
17
通过率
41.2%

相关标签

相关企业

提示 1
Could you use a data structure to easily store and modify the list of applications running on a machine?

提示 2
Use a map or ordered set to store applications on a machine in the order they are added.

提示 3
We would also like to be able to efficiently find the machine with the largest remaining capacity. What data structure allows us to do this?

提示 4
We can use a heap data structure to store the machines with their corresponding remaining capacities.

相似题目

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
["DCLoadBalancer","addMachine","addMachine","addMachine","addMachine","addApplication","addApplication","addApplication","addApplication","getApplications","addMachine","addApplication","stopApplication","addApplication","getApplications","removeMachine","getApplications"]
[[],[1,1],[2,10],[3,10],[4,15],[1,3],[2,11],[3,6],[4,5],[2],[5,10],[5,5],[3],[6,5],[4],[4],[2]]
Source