Maximum Performance of a Team

Hard Heaps Top K / Kth Largest / Smallest Original on LeetCode

Reported atPhonePe

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^5
  • speed.length == efficiency.length == n
  • 1 <= speed[i] <= 10^5
  • 1 <= 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.

00:00
Testcase

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.

ApproachTimeSpace
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)

TimeO(n² log n)For each of the n engineers we gather and sort up to n speeds.
SpaceO(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.

  1. For each i: collect the speeds of all j != i with efficiency[j] >= efficiency[i].
  2. Sort them in descending order and add the first k - 1 of them to speed[i].
  3. Candidate = that sum * efficiency[i]; keep the best.
  4. 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);
    }
}

2Optimal (sort by efficiency + min-heap of speeds)

TimeO(n log n)Sorting costs n log n, and each engineer is pushed and popped at most once in a heap of size k.
SpaceO(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.

  1. Pair (efficiency, speed) and sort by efficiency descending.
  2. For each pair: push speed into a min-heap and add it to sum.
  3. If the heap size exceeds k, pop the smallest speed and subtract it.
  4. best = max(best, sum * efficiency).
  5. 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);
    }
}

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.