Watch 10 video solutions for Maximum Performance of a Team, a hard level problem involving Array, Greedy, Sorting. This walkthrough by NeetCode has 35,895 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 ith 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 109 + 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 <= 105speed.length == nefficiency.length == n1 <= speed[i] <= 1051 <= efficiency[i] <= 108Problem Overview: You are given two arrays representing the speed and efficiency of engineers. Select at most k engineers such that the team's performance, defined as (sum of speeds) * (minimum efficiency), is maximized. The challenge comes from the fact that adding a faster engineer may reduce the minimum efficiency of the team.
Approach 1: Brute Force Team Enumeration (Exponential Time)
The most straightforward idea is to try every possible subset of engineers of size up to k. For each subset, compute the total speed and find the minimum efficiency, then calculate performance. This requires generating combinations and evaluating each one. The time complexity grows exponentially, roughly O(C(n, k) * k), and the space complexity is O(k) for subset tracking. This approach quickly becomes infeasible when n grows beyond small limits, but it helps clarify the relationship between speed sum and minimum efficiency.
Approach 2: Dynamic Programming with Binary Search (O(n log n + nk))
Another idea uses dynamic programming combined with sorting. First, sort engineers by efficiency in descending order using sorting. When efficiency is fixed at position i, it acts as the minimum efficiency of the team. You then choose up to k-1 engineers from earlier elements that maximize the speed sum. A DP structure or maintained ordered set with binary search can help track candidate speed sums. The time complexity is about O(n log n + nk) depending on implementation, and space complexity is O(n). This method demonstrates the core insight that efficiency should be treated as the limiting factor.
Approach 3: Greedy with Min-Heap for Speed (Optimal) (O(n log k))
The optimal solution uses a greedy strategy with a heap (priority queue). First, pair each engineer's efficiency and speed, then sort engineers by efficiency in descending order. When processing engineer i, treat their efficiency as the minimum efficiency of the team. Maintain a running sum of speeds for the current team. Use a min-heap to keep the fastest k engineers seen so far: push the current speed, and if the heap size exceeds k, remove the smallest speed. This ensures the team always keeps the largest speed sum possible while the current efficiency acts as the bottleneck. At each step, compute performance = speedSum * currentEfficiency and update the maximum.
This greedy insight works because the minimum efficiency always comes from the engineer with the lowest efficiency in the selected group. By iterating efficiencies in descending order, that value becomes fixed for the current iteration, allowing you to focus on maximizing the speed sum using a heap. The algorithm runs in O(n log k) time due to heap operations and O(k) space.
Recommended for interviews: The greedy + min-heap solution is what interviewers expect. It demonstrates understanding of greedy algorithms, efficient data structures, and the ability to transform a complex objective into a sorted traversal problem. Mentioning the brute force idea shows reasoning, but implementing the heap-based solution proves strong algorithmic skill.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Subset Enumeration | O(C(n,k) * k) | O(k) | Conceptual understanding or very small input sizes |
| Dynamic Programming with Binary Search | O(n log n + nk) | O(n) | Exploring structured optimizations after sorting by efficiency |
| Greedy with Min-Heap for Speed | O(n log k) | O(k) | Optimal solution for large inputs and typical interview expectations |