Maximum Performance of a Team - Problem

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 109 + 7.

Input & Output

Example 1 — Basic Case
$ Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
Output: 60
💡 Note: We choose engineers 2 and 5 (0-indexed) with speeds [10,5] and efficiencies [4,7]. Performance = (10+5) × min(4,7) = 15 × 4 = 60.
Example 2 — Single Engineer
$ Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
Output: 68
💡 Note: Choose engineer with efficiency 9 and speed 1, plus top 2 others by speed considering efficiency constraint. Best combination gives performance 68.
Example 3 — All Engineers
$ Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 6
Output: 72
💡 Note: When k equals n, we can consider all engineers. The optimal team maximizes sum of speeds × minimum efficiency across all possible combinations.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ speed[i] ≤ 105
  • 1 ≤ efficiency[i] ≤ 108
  • 1 ≤ k ≤ n

Visualization

Tap to expand
Maximum Performance of a Team INPUT n = 6 engineers, k = 2 Speed: 2 10 3 1 5 8 Efficiency: 5 4 3 9 7 2 Engineers (speed, efficiency): 1 (2,5) 2 (10,4) 3 (3,3) 4 (1,9) 5 (5,7) 6 (8,2) Performance Formula: sum(speeds) x min(efficiency) ALGORITHM STEPS 1 Sort by Efficiency (desc) [(1,9),(5,7),(2,5),(10,4),(3,3),(8,2)] 2 Use Min-Heap for speeds Keep track of k largest speeds 3 Iterate and Calculate Current eff is always minimum Eng Eff Heap Sum Perf 4 9 [1] 1 9 5 7 [1,5] 6 42 1 5 [5,2] 7 35 2 4 [5,10] 15 60 3 3 [10,3] 13 39 6 2 [10,8] 18 36 * Best: 60 (engineers 2,5) 4 Track Maximum Update max at each step FINAL RESULT Optimal Team Selected: Eng 2 s=10 Eng 5 s=5 Calculation: Speed sum = 10 + 5 = 15 Min efficiency = min(4,7) = 4 Performance = 15 x 4 = 60 OUTPUT 60 OK - Maximum Performance Found! Key Insight: Sort engineers by efficiency in descending order. As we iterate, current engineer has the minimum efficiency. Use a min-heap to maintain the k highest speeds seen so far. Remove smallest speed when heap exceeds k. This greedy approach ensures we always consider optimal combinations. TutorialsPoint - Maximum Performance of a Team | Greedy with Sorting and Heap
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
156.0K Views
Medium Frequency
~35 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen