Rearrange Server Load
Given two arrays, serverCapacity, and serverLoad, each of size n, they represent the resource requirements or capacities of servers and the workload distribution across the servers. The total resource consumption is the sum of serverCapacity[i] * serverLoad[i] (0 ≤ i < n).
The task is to rearrange the array serverLoad, redistributing the load across servers to minimize the total resource consumption, ensuring their efficient utilization. Return this optimal arrangement. In scenarios with multiple solutions, return the lexicographically smallest array of serverLoad.
Complete the function rearrangeServerLoad in the editor.
rearrangeServerLoad has the following parameters:
- 1.
int[] serverCapacity: an array of integers representing server capacities - 2.
int[] serverLoad: an array of integers representing server loads
Returns
int[]: the rearranged array of server loads that minimizes the total resource consumption
◝(ᵔᵕᵔ)◜ Many thanks to the amazing spike! ᯓ ᡣ𐭩
1Example 1
The task is to rearrange the elements in the array serverLoad to minimize the sum, serverCapacity[i] * serverLoad[i].
If serverLoad = [3, 2, 1], the total resources are calculated as follows: 4 * 3 + 5 * 2 + 6 * 1 = 28. This arrangement yields the lowest sum of resources.
2Example 2
For the rearranged serverLoad = [6, 5, 2, 2, 4], the total resource requirement is calculated as follows: = 1 * 6 + 2 * 5 + 3 * 2 + 3 * 2 + 3 * 4 = 40. 40 is the minimum possible sum after rearranging the serverLoad array, hence the answer.