3961. Maximize Sum of Device Ratings
Description
You are given a 2D integer array units of size m Γ n where units[i][j] represents the capacity of the jth unit in the ith device. Each device contains exactly n units.
The rating of a device is the minimum capacity among all its units.
You may perform the following operation any number of times (including zero):
- Choose a device
ithat has not been used as a source before. - Remove exactly one unit from device
iand add it to any different device. - Then mark device
ias used, so it cannot be chosen again as a source.
Return the maximum possible sum of the ratings of all devices after any number of such operations.
Note:
- Devices can receive units from multiple devices, regardless of whether they have been selected.
- The rating of an empty device is 0.
Β
Example 1:
Input: units = [[1,3],[2,2]]
Output: 4
Explanation:
- ββββββββββββββSelect device
i = 0and transferunits[0][0] = 1to devicei = 1. - After the transfer, the ratings are:
- Device
0 = [3]:rating[0] = 3 - Device
1 = [2, 2, 1]:rating[1] = 1
- Device
- Thus, the sum of ratings is
3 + 1 = 4.
Example 2:
Input: units = [[1,2,3],[4,5,6]]
Output: 6
Explanation:
- Select device
i = 1and transferunits[1][0] = 4to devicei = 0. - After the transfer, the ratings are:
- Device
0 = [1, 2, 3, 4]:rating[0] = 1 - Device
1 = [5, 6]:rating[1] = 5
- Device
- Thus, the sum of ratings is
1 + 5 = 6.
Example 3:
Input: units = [[5,5,5],[1,1,1]]
Output: 6
Explanation:
- No transfers increase the sum of ratings. Thus, the sum of ratings is
5 + 1 = 6.
Β
Constraints:
1 <= m == units.length <= 1051 <= n == units[i].length <= 105m * n <= 2 * 1051 <= units[i][j] <= 105
Solutions
Solution 1: Greedy
Adding a unit to a device can only decrease or keep its rating unchanged. Therefore, if \(n = 1\), we can directly return the sum of all device ratings.
Otherwise, we sort the units of each device in ascending order, take the smallest unit from each device, and concentrate them into one device with rating \(\textit{mn}\). If we concentrate them into device \(i\), the rating of device \(i\) changes from the second smallest value \(\textit{mn2}\) to \(\textit{mn}\), so the total rating decreases by \(\textit{mn2} - \textit{mn}\). To maximize the total rating, we should choose the device with the smallest decrease, i.e., the device with the smallest \(\textit{mn2}\).
The time complexity is \(O(m \times n)\), where \(m\) and \(n\) are the number of devices and the number of units per device, respectively. The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |