Maximum Performance of a Team Problem
Description
LeetCode Problem 1383.
You are given two integers n and k and two integer arrays speed and efficiency both of length n. There are n engineers numbered from 1 to n. speed[i] and efficiency[i] represent the speed and efficiency of the i^th engineer respectively.
Choose at most k different engineers out of the n engineers to form a team with the maximum performance.
The performance of a team is the sum of its engineers' speeds multiplied by the minimum efficiency among its engineers.
Return the maximum performance of this team. Since the answer can be a huge number, return it modulo 10^9 + 7.
Example 1:
Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
Output: 60
Explanation:
We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.
Example 2:
Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
Output: 68
Explanation:
This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.
Example 3:
Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4
Output: 72
Constraints:
1 <= k <= n <= 10^5speed.length == nefficiency.length == n1 <= speed[i] <= 10^51 <= efficiency[i] <= 10^8
Solution
Approach: Sort by Efficiency and Keep the Fastest k
The performance depends on the team's minimum efficiency, so try each engineer as the least efficient member of the team. If engineer i has the minimum efficiency e, everyone else on the team must have efficiency at least e, and to make the speed sum as big as possible we pick the fastest of them, up to k people in total.
Sort the engineers by efficiency, highest first, and walk through them:
- Add the current engineer's speed to a min-heap and to a running total
total. - If the heap now holds more than
kspeeds, remove the smallest one and subtract it fromtotal. - Everyone in the heap has efficiency at least the current one, so
total * eis a valid team's performance. Keep the maximum.
The current engineer may be the one we just removed (when it is the slowest). Then the heap holds a team whose real minimum efficiency is at least e, so total * e only underestimates that team's performance and never exceeds the true maximum.
Two details are easy to get wrong:
- The product can reach about 1010 * 108 = 1018, so use 64-bit integers (BigInt in JavaScript, where doubles are only exact up to 253).
- Take the maximum on the exact values and apply the modulo
10^9 + 7only at the end: comparing values after the modulo would pick the wrong team.
Complexity: O(n log n) time for sorting and the heap operations, and O(n) extra space.
import heapq
class Solution:
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
# Try each engineer, from the most efficient down, as the team's least efficient member.
engineers = sorted(zip(efficiency, speed), reverse=True)
team = [] # min-heap of the speeds of the (at most k) fastest engineers seen so far
total = best = 0
for e, s in engineers:
heapq.heappush(team, s)
total += s
if len(team) > k:
total -= heapq.heappop(team) # drop the slowest
best = max(best, total * e)
return best % (10 ** 9 + 7) # take the max first, the modulo only at the end