Maximum Performance of a Team
Maximum Performance of a Team is a hard heaps problem solved with the top k / kth largest / smallest pattern.
The best approach, optimal (sort by efficiency + min-heap of speeds), runs in O(n log n) time and O(n) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (fix the minimum, pick the fastest) up.
Problem
There are n engineers numbered 0 to n - 1. Engineer i has a speed speed[i] and an efficiency efficiency[i]. You may pick a team of at most k engineers (at least one).
The performance of a team is the sum of its members’ speeds multiplied by the smallest efficiency among its members. Return the maximum performance any team can reach. Since it can be very large, return it modulo 10^9 + 7 (maximize the true value first, then take the remainder).
Examples
Example 1
- Input
n = 5, speed = [4, 9, 2, 6, 3], efficiency = [7, 2, 8, 5, 6], k = 2- Output
50- Why
- Engineers 0 and 3 give (4 + 6) * min(7, 5) = 50. Taking engineer 1 for its speed 9 would drag the minimum efficiency down to 2.
Example 2
- Input
n = 4, speed = [3, 5, 1, 2], efficiency = [10, 4, 9, 8], k = 3- Output
48- Why
- Engineers 0, 2 and 3 have total speed 6 and minimum efficiency 8, giving 48. Adding engineer 1 instead of engineer 2 would lift the speed to 10 but drop the minimum to 4, giving only 40.
Example 3
- Input
n = 3, speed = [8, 8, 8], efficiency = [3, 3, 3], k = 3- Output
72- Why
- All three are equal, so take everyone: 24 * 3 = 72.
Constraints
1 <= k <= n <= 10^5speed.length == efficiency.length == n1 <= speed[i] <= 10^51 <= efficiency[i] <= 10^8
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (fix the minimum, pick the fastest) | O(n² log n) | O(n) |
| Optimal (sort by efficiency + min-heap of speeds) | O(n log n) | O(n) |
1Brute force (fix the minimum, pick the fastest)
O(n² log n)For each of the n engineers we gather and sort up to n speeds.O(n)One temporary list of speeds.Try every engineer i as the member with the smallest efficiency. The other members may be anyone with efficiency at least efficiency[i]; to maximize the sum, take the k - 1 fastest of them. Compute the performance with 64-bit arithmetic and keep the maximum, then apply the modulo.
- For each i: collect the speeds of all j != i with efficiency[j] >= efficiency[i].
- Sort them in descending order and add the first k - 1 of them to speed[i].
- Candidate = that sum * efficiency[i]; keep the best.
- Return best % (10^9 + 7).
class Solution {
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
long best = 0;
for (int i = 0; i < n; i++) {
List<Integer> others = new ArrayList<>();
for (int j = 0; j < n; j++)
if (j != i && efficiency[j] >= efficiency[i]) others.add(speed[j]);
others.sort(Collections.reverseOrder());
long sum = speed[i];
for (int t = 0; t < Math.min(k - 1, others.size()); t++) sum += others.get(t);
best = Math.max(best, sum * efficiency[i]);
}
return (int) (best % 1_000_000_007L);
}
}class Solution:
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
best = 0
for i in range(n):
others = [speed[j] for j in range(n) if j != i and efficiency[j] >= efficiency[i]]
others.sort(reverse=True)
total = speed[i] + sum(others[:k - 1])
best = max(best, total * efficiency[i])
return best % 1_000_000_007class Solution {
public:
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
long long best = 0;
for (int i = 0; i < n; i++) {
vector<int> others;
for (int j = 0; j < n; j++)
if (j != i && efficiency[j] >= efficiency[i]) others.push_back(speed[j]);
sort(others.rbegin(), others.rend());
long long sum = speed[i];
for (int t = 0; t < min(k - 1, (int)others.size()); t++) sum += others[t];
best = max(best, sum * efficiency[i]);
}
return (int)(best % 1000000007LL);
}
};function maxPerformance(n, speed, efficiency, k) {
let best = 0n;
for (let i = 0; i < n; i++) {
const others = [];
for (let j = 0; j < n; j++)
if (j !== i && efficiency[j] >= efficiency[i]) others.push(speed[j]);
others.sort((a, b) => b - a);
let sum = speed[i];
for (let t = 0; t < Math.min(k - 1, others.length); t++) sum += others[t];
const perf = BigInt(sum) * BigInt(efficiency[i]);
if (perf > best) best = perf;
}
return Number(best % 1000000007n);
}2Optimal (sort by efficiency + min-heap of speeds)
O(n log n)Sorting costs n log n, and each engineer is pushed and popped at most once in a heap of size k.O(n)The sorted pairs and a heap of up to k speeds.Process engineers in order of decreasing efficiency. When engineer i is processed, its efficiency is the minimum among everyone seen so far, so the best team with i as the weakest link is i plus the fastest k - 1 earlier engineers. Maintain the k largest speeds seen so far (including i) in a min-heap with a running sum: push the new speed, and if the heap grows past k, pop the slowest. If engineer i itself gets popped, the heap holds a team whose real minimum efficiency is at least efficiency[i], so the product still never overstates a real team's performance; and the team that truly is best gets counted exactly when its weakest member is processed. Track the maximum as a 64-bit value.
- Pair (efficiency, speed) and sort by efficiency descending.
- For each pair: push speed into a min-heap and add it to sum.
- If the heap size exceeds k, pop the smallest speed and subtract it.
- best = max(best, sum * efficiency).
- Return best % (10^9 + 7).
class Solution {
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
Integer[] order = new Integer[n];
for (int i = 0; i < n; i++) order[i] = i;
Arrays.sort(order, (a, b) -> Integer.compare(efficiency[b], efficiency[a]));
PriorityQueue<Integer> heap = new PriorityQueue<>();
long sum = 0, best = 0;
for (int i : order) {
heap.offer(speed[i]);
sum += speed[i];
if (heap.size() > k) sum -= heap.poll();
best = Math.max(best, sum * efficiency[i]);
}
return (int) (best % 1_000_000_007L);
}
}class Solution:
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
order = sorted(range(n), key=lambda i: -efficiency[i])
heap = []
total = 0
best = 0
for i in order:
heapq.heappush(heap, speed[i])
total += speed[i]
if len(heap) > k:
total -= heapq.heappop(heap)
best = max(best, total * efficiency[i])
return best % 1_000_000_007class Solution {
public:
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
vector<int> order(n);
for (int i = 0; i < n; i++) order[i] = i;
sort(order.begin(), order.end(), [&](int a, int b) { return efficiency[a] > efficiency[b]; });
priority_queue<int, vector<int>, greater<int>> heap;
long long sum = 0, best = 0;
for (int i : order) {
heap.push(speed[i]);
sum += speed[i];
if ((int)heap.size() > k) {
sum -= heap.top();
heap.pop();
}
best = max(best, sum * efficiency[i]);
}
return (int)(best % 1000000007LL);
}
};function maxPerformance(n, speed, efficiency, k) {
const order = Array.from({ length: n }, (_, i) => i).sort((a, b) => efficiency[b] - efficiency[a]);
const heap = [];
const push = (x) => {
heap.push(x);
let i = heap.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (heap[p] <= heap[i]) break;
[heap[p], heap[i]] = [heap[i], heap[p]];
i = p;
}
};
const pop = () => {
const top = heap[0], last = heap.pop();
if (heap.length > 0) {
heap[0] = last;
let i = 0;
while (true) {
const l = 2 * i + 1, r = l + 1;
let m = i;
if (l < heap.length && heap[l] < heap[m]) m = l;
if (r < heap.length && heap[r] < heap[m]) m = r;
if (m === i) break;
[heap[m], heap[i]] = [heap[i], heap[m]];
i = m;
}
}
return top;
};
let sum = 0;
let best = 0n;
for (const i of order) {
push(speed[i]);
sum += speed[i];
if (heap.length > k) sum -= pop();
const perf = BigInt(sum) * BigInt(efficiency[i]);
if (perf > best) best = perf;
}
return Number(best % 1000000007n);
}Edge cases to test
k = 1: the best single engineer by speed * efficiency- A team smaller than k is better than a full one when the extra members have low efficiency
- Many engineers sharing the same efficiency
- The true maximum can reach about 10^18 (total speed up to 10^10 times efficiency up to 10^8): compare raw products in 64-bit arithmetic and apply the modulo only once at the end
Hints
Hint 1
Fix the engineer with the lowest efficiency in the team. Then every other member must have efficiency at least that high, and among those you simply want the fastest.
Hint 2
Sort engineers by efficiency from high to low. When you reach engineer i, everyone seen so far is an allowed teammate.
Hint 3
Keep the k largest speeds seen so far in a min-heap together with their sum. Each step, the candidate is (sum of the heap) * efficiency[i].
FAQ
What is the best time complexity for Maximum Performance of a Team?
Optimal (sort by efficiency + min-heap of speeds) runs in O(n log n) time and O(n) extra space. Sorting costs n log n, and each engineer is pushed and popped at most once in a heap of size k.
Which pattern does Maximum Performance of a Team use?
It is a heaps problem that uses the top k / kth largest / smallest pattern. Other problems with the same pattern: Kth Largest Element in an Array, Kth Largest Element in a Stream, Top K Frequent Words.
Is there a brute force solution for Maximum Performance of a Team?
Yes. Brute force (fix the minimum, pick the fastest) takes O(n² log n) time and O(n) space. Try every engineer i as the member with the smallest efficiency.
Which edge cases should I test for Maximum Performance of a Team?
k = 1: the best single engineer by speed efficiency; A team smaller than k is better than a full one when the extra members have low efficiency; Many engineers sharing the same efficiency; The true maximum can reach about 10^18 (total speed up to 10^10 times efficiency up to 10^8): compare raw products in 64-bit arithmetic and apply the modulo only once at the end.