Solution: Maximum Performance of a Team
Let’s solve the Maximum Performance of a Team problem using the Top K Elements pattern.
We'll cover the following
Statement
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
. The value speed[i]
represents the speed of the i-th
engineer, and efficiency[i]
represents their efficiency.
To form a team with the maximum performance, you need to select at most k
different engineers from the n
engineers.
The performance of a team is calculated as follows:
The sum of the selected engineers’ speeds
Return the maximum performance of the team. As the result can be a very large number, return it modulo
Constraints:
k
n
speed.length
n
efficiency.length
n
speed[i]
efficiency[i]
Solution
The core idea behind the solution is to combine the Top-K Elements pattern with a greedy strategy to maximize the overall team performance. As a team's performance is calculated as the sum of selected engineers’ speeds multiplied by the minimum efficiency among them, the goal is to form a group of up to k
engineers with the highest speeds, while keeping the lowest efficiency in the group as high as possible. To achieve this, all engineers are sorted in descending order of efficiency. This guarantees that as we iterate through the list, the current engineer’s efficiency becomes the lowest efficiency for any team that includes them and any previously considered engineers, because all subsequent engineers have equal or lower efficiency.
We use a min heap to maintain the top k
speeds, which efficiently keeps track of the smallest speed in the current selection. The smallest speed is removed if the heap grows beyond k
, ensuring only the most impactful speeds are retained. As we process each engineer, we update the total speed and calculate the potential performance by multiplying it by the current engineer’s efficiency. The highest performance during this process is recorded and returned at the end.
The steps of the algorithm are as follows:
Combine
efficiency[i]
andspeed[i]
into tuples as(efficiency, speed)
and sort theengineers
list in descending order ofefficiency
to process higher-efficiency engineers first.Initialize the following variables:
maxPerf
: To keep track of the maximum performance encountered so far.speedSum
: A running total of the speeds of engineers currently in the team.minHeap
: A min heap used to store the topk
speeds efficiently, allowing fast removal of the smallest speed when needed.
Iterate through the sorted
engineers
. For each(eff, spd)
tuple in the sorted list:If the heap already contains
k
elements, remove the smallest speed to make room for the current engineer’s speed, and subtract it from the running total (speedSum
). This ensures that the team always contains at mostk
engineers.Push the current engineer’s
spd
intominHeap
and updatespeedSum
by addingspd
.Compute performance as
speedSum * eff
, whereeff
is the efficiency of the current engineer (who is now the least efficient in the current team).Update
maxPerf
if the newly computed performance is higher than the current maximum.
Return
maxPerf % (10^9 + 7)
to ensure the result stays within standard integer bounds, especially for large inputs.
Let’s look at the illustration below to better understand the solution.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.