FastPrepFastPrep
Problem Brief

Rearrange Server Load

NEW GRADOA

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.

Function Description

Complete the function rearrangeServerLoad in the editor.

rearrangeServerLoad has the following parameters:

  1. 1. int[] serverCapacity: an array of integers representing server capacities
  2. 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

Input
serverCapacity = [4, 5, 6], serverLoad = [1, 2, 3]
Output
[3, 2, 1]
Explanation

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

Input
serverCapacity = [1, 2, 3, 3, 3], serverLoad = [2, 2, 4, 5, 6]
Output
[6, 5, 2, 2, 4]
Explanation

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.

public int[] rearrangeServerLoad(int[] serverCapacity, int[] serverLoad) {
  // write your code here
}
Input

serverCapacity

[4, 5, 6]

serverLoad

[1, 2, 3]

Output

[3, 2, 1]

Sign in to submit your solution.