↓Skip to main content
  1. LeetCode/

LeetCode 3961: Maximize Sum of Device Ratings

·2 mins· ·
LeetCode Medium Greedy Sorting Matrix
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-06-18

  • Submissions: Wrong Answer → Accepted

Approach
#

A device’s rating is its smallest unit. Donating that unit raises the rating to the second-smallest value, never lowering it.

  • Each device donates at most once, so it can rise at most to its second-smallest value.
  • Received units drag the receiver’s rating down. Send every donation to the same device and sacrifice only that one: its rating becomes the smallest of all minimums (the global minimum).
  • Which one to sacrifice? Every other device gets its second-smallest value, so sacrifice the one whose second-smallest is smallest.

The answer is sum of second-smallest values - smallest second-smallest + global minimum. With one unit per device, donating empties the device (rating 0), so do nothing and return the sum of all units. This is the pairing idea from Greedy Template 4.

My Attempts
#

Attempt 1 took unit[0] as the minimum and unit[1] as the second-smallest, assuming each row was sorted. For [[4, 3]] a single device cannot donate, so the answer is 3; it returned 4.

Finding each row’s minimum, removing it, then finding the second-smallest passed.

Solution
#

class Solution:
    def maxRatings(self, units: List[List[int]]) -> int:
        if len(units[0]) == 1:
            return sum(u[0] for u in units)
        global_min, total, min_second = inf, 0, inf
        for u in units:
            a, b = heapq.nsmallest(2, u)
            global_min = min(global_min, a)
            total += b
            min_second = min(min_second, b)
        return total - min_second + global_min

With a single device the formula gives b - b + a = a, exactly the do-nothing answer.

Complexity
#

Time O(m · n), space O(1).

Mistakes to Avoid
#

  • Rows are not sorted; find the smallest and second-smallest yourself.
  • Special-case one unit per device.

Interview Explanation
#

Donating the smallest unit lifts a rating to the second-smallest value, so all devices but one donate, all to the same device, sacrificing only it. The sacrificed device ends at the global minimum, so choose the one with the smallest second-smallest value. The answer is the sum of second-smallest values minus the smallest of them plus the global minimum. Time O(m · n).

Related

LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them
LeetCode 3075: Maximize Happiness of Selected Children
·1 min
LeetCode Medium Array Greedy Sorting
LeetCode 3075 note: choose k children from the happiest, where the i-th choice loses i, floored at 0
LeetCode 324: Wiggle Sort II
·2 mins
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
LeetCode 324 note: sort, split into a small half and a large half, reverse each, and interleave with small values at even indices and large ones at odd indices
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest