Maximum Performance of a Team
Master this topic with zero to advance depth.
Maximum Performance of a Team
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 engineers out of the n engineers to form a team with the maximum performance.
The performance of a team is the sum of their speeds multiplied by the minimum efficiency among their engineers.
Return the maximum performance of this team. Since the answer can be a huge number, return it modulo 10^9 + 7.
Visual Representation
n=6, k=2, speed=[2,10,3,1,5,8], efficiency=[5,4,3,9,7,2]
Key Insight: Sort by efficiency descending.
For each engineer i as the "min efficiency" of the group,
pick the k fastest engineers from 0..i (including i).
Sorted by eff desc: (9,1),(7,5),(5,2),(4,10),(3,3),(2,8)
- Min eff = 9: Team = [(1,9)]. Perf = 1*9 = 9
- Min eff = 7: Team = [(1,9),(5,7)]. Sum speeds=6. Perf=6*7=42
- Min eff = 5: Team = [(2,5),(5,7),(1,9)]. Speeds=[1,5,2]. Sum k=2 fastest=5+2=7. Perf=7*5=35
- Min eff = 4: Team picks (10,4) as one, need 2 fastest. Max perf = (10+5)*4 = 60Examples
Level I: Brute Force - Check All Subsets
Intuition
Try all possible subsets of at most k engineers from the n engineers. For each subset, compute performance = (sum of speeds) * (minimum efficiency). Return the maximum. This is correct but exponential in complexity.
Detailed Dry Run
n=4, k=2, speed=[2,10,3,1], efficiency=[5,4,3,9] Subsets of size 1: {0}->25=10, {1}->104=40, {2}->33=9, {3}->19=9 Subsets of size 2: {0,1}->12min(5,4)=48, {0,2}->5min(5,3)=15, {0,3}->3min(5,9)=15, {1,2}->13min(4,3)=39, {1,3}->11min(4,9)=44, {2,3}->4min(3,9)=12 Max = 48
Level II: Sort by Efficiency and Search K-Fastest
Intuition
To improve on brute force, we can sort the engineers by their efficiency in descending order. By doing so, for each engineer i, if we consider them as the minimum efficiency in the team, we only need to look at engineers from 0 to i (who all have efficiency >= eff[i]) and pick the k fastest ones.
Detailed Dry Run
n=4, k=2, speed=[2,10,3,1], efficiency=[5,4,3,9] Sorted: (9,1), (5,2), (4,10), (3,3)
- i=0, Eff=9: prefix=[1]. Sum k=2 fastest = 1. Perf = 1*9=9.
- i=1, Eff=5: prefix=[1,2]. Sum k=2 fastest = 3. Perf = 3*5=15.
- i=2, Eff=4: prefix=[1,2,10]. Sum k=2 fastest = 12. Perf = 12*4=48.
- i=3, Eff=3: prefix=[1,2,10,3]. Sum k=2 fastest = 13. Perf = 13*3=39. Max = 48.
Level III: Sort by Efficiency + Min-Heap (Optimal)
Intuition
Key Insight: When we pick any subset of engineers, the performance is bottlenecked by the minimum efficiency. If we fix the minimum efficiency to be efficiency[i], then we should include engineer i in the team AND pick the k-1 fastest engineers from all engineers who have efficiency >= efficiency[i].
If we sort engineers by efficiency in descending order, as we iterate through them, the current engineer i has the minimum efficiency seen so far. We need the maximum sum of speeds from engineer 0 to i. We maintain a Min-Heap of size k storing speeds — when the heap exceeds k, we pop the smallest speed. This ensures our heap always holds the k largest speeds.
Detailed Dry Run
n=6, k=2, sp=[2,10,3,1,5,8], eff=[5,4,3,9,7,2] Sort by eff desc: [(9,1),(7,5),(5,2),(4,10),(3,3),(2,8)] min_heap=[], speed_sum=0, best=0
- (9,sp=1): push 1. sum=1. size=1<=2. perf=1*9=9. best=9.
- (7,sp=5): push 5. sum=6. size=2<=2. perf=6*7=42. best=42.
- (5,sp=2): push 2. sum=8. size=3>2. pop min(1). sum=7. perf=7*5=35. best=42.
- (4,sp=10): push 10. sum=17. size=3>2. pop min(2). sum=15. perf=15*4=60. best=60.
- (3,sp=3): push 3. sum=18. size=3>2. pop min(3). sum=15. perf=15*3=45. best=60. Return 60 % MOD = 60
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.