Maximum Performance of a Team

Updated on 12 June, 2025
Maximum Performance of a Team header image

Problem Statement

In this problem, you are provided with two key integers, n and k, representing the number of engineers and the maximum number of engineers you can select to form a team, respectively. Additionally, you are given two integer arrays, speed and efficiency, each of size n, where speed[i] denotes the speed of the ith engineer and efficiency[i] indicates the ith engineer's efficiency.

The task is to form a team of engineers such that the team's performance is maximized. The performance of a team is calculated as the sum of the speeds of all selected engineers multiplied by the minimum efficiency of those selected engineers.

The output should be the maximum possible performance of the team, and since large numbers might be involved in calculations, the result should be returned modulo 10^9 + 7.

Examples

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 <= 105
  • speed.length == n
  • efficiency.length == n
  • 1 <= speed[i] <= 105
  • 1 <= efficiency[i] <= 108

Approach and Intuition

To solve the problem of choosing a team with maximum performance, an effective approach revolves around understanding the relationship between the speed and efficiency parameters and how team composition affects the performance metric.

  1. Sort Engineers by Efficiency: Priority should be given to maintaining a higher minimum efficiency in the group, as the performance metric is directly multiplied by the smallest efficiency of any selected engineer. Hence, sorting engineers by efficiency in descending order allows us to always consider the next least efficient engineer while iterating.

  2. Using a Priority Queue for Speed Management: To keep track of the top speeds while potentially removing the lowest contributing speeds, a priority queue (or min-heap) can be utilized. This way, as we iterate through the sorted list of engineers:

    • Add the current engineer's speed to the heap.
    • If the heap's size exceeds k, remove the smallest element (the least speed) which ensures that only the top k speeds are considered at any point.
  3. Calculate Performance Continuously: As each engineer is considered (in decreasing order of efficiency), calculate the potential team performance by taking the sum of the speeds from the priority queue (representing the top speeds up to that point) multiplied by the current engineer’s efficiency (since it would be the lowest efficiency if this engineer was included in the team).

  4. Track the Maximum Performance: Throughout the process, continuously update the maximum performance encountered.

  5. Modulo Operation: Given the potentially large numbers involved, apply the modulo 10^9 + 7 operation to the results to prevent overflow and meet problem constraints.

By implementing the above approach, the solution efficiently iterates through the engineers while maintaining control over which engineers' speeds contribute to the team's overall speed sum through the use of a heap, and by anchoring calculations to the incremental least efficiencies, ensuring that the maximum performance is correctly identified.

Solutions

  • Java
  • Python
java
class Solution {
    public int highestPerformance(int count, int[] speeds, int[] effs, int limit) {
        int mod = (int)1e9 + 7;
        // generate list of pairs
        List<Pair<Integer, Integer>> engs = new ArrayList<>();
        for (int i = 0; i < count; ++i) {
            engs.add(new Pair(effs[i], speeds[i]));
        }
        // sort by efficiency decreasing
        Collections.sort(engs, Comparator.comparing(o -> -o.getKey()));

        // use a priority queue to keep the top speeds
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(Comparator.naturalOrder());

        long sumSpeed = 0, maxPerformance = 0;
        for (Pair<Integer, Integer> eng : engs) {
            Integer eff = eng.getKey();
            Integer spd = eng.getValue();
            // maintain the heap with the top speeds
            if (minHeap.size() > limit - 1) {
                sumSpeed -= minHeap.poll();
            }
            minHeap.add(spd);

            // calculate the max performance with the current minimum efficiency
            sumSpeed += spd;
            maxPerformance = Math.max(maxPerformance, sumSpeed * eff);
        }
        return (int) (maxPerformance % mod);
    }
}

In this Java solution to calculate the maximum performance of a team, the problem is approached by combining engineers' efficiencies and speeds using pairing and sorting techniques along with a priority queue to manage the top performing team dynamically.

  • Start by declaring mod as (10^9 + 7) to facilitate the calculation of large numbers without overflow.
  • Create a list of pairs (engs) to store each engineer’s efficiency and speed, which is then filled by iterating through the given inputs.
  • Sort this list of pairs in decreasing order based on efficiency to prioritize higher efficiencies.
  • Utilize a PriorityQueue (minHeap) to store the speeds of the engineers. This queue helps in efficiently retrieving and updating the smallest speed when the size exceeds the limit of team members allowed.
  • Define variables sumSpeed and maxPerformance to keep track of the cumulative speed of the selected engineers and the maximum performance encountered, respectively.
  • Iterate over the sorted list of engineers. For each engineer, check the size of the priority queue. If it exceeds the limit (limit - 1), remove the smallest speed from the queue (and sumSpeed) to ensure only the top speeds are considered.
  • Add the current engineer’s speed to the minHeap and update sumSpeed.
  • Calculate the current performance as the product of sumSpeed and the current minimum efficiency (derived from the sorted order). Update maxPerformance if this new value is greater.
  • Finally, return the maximum performance modulo mod to ensure the output is within the standard integer limits.

This approach ensures that at every step, the team is configured to max out its performance based on the constraints of maximum team members and the available efficiencies and speeds.

python
class Solution:
    def maximumPerformance(self, num: int, speeds: List[int], efficiencies: List[int], limit: int) -> int:
        MOD = 10**9 + 7

        # Create list of tuples (efficiency, speed)
        engineerData = zip(efficiencies, speeds)
        # Sort data primarily by efficiency
        engineerData = sorted(engineerData, key=lambda x:x[0], reverse=True)

        speedPriorityQueue = []
        currentSpeedSum, maxPerformance = 0, 0
        for eff, spd in engineerData:
            # Use min-heap to track the top speeds
            if len(speedPriorityQueue) > limit - 1:
                currentSpeedSum -= heapq.heappop(speedPriorityQueue)
            heapq.heappush(speedPriorityQueue, spd)

            # Update current team speed sum and calculate performance
            currentSpeedSum += spd
            maxPerformance = max(maxPerformance, currentSpeedSum * eff)

        return maxPerformance % MOD

The solution outlines the approach to solve the problem of finding the maximum performance of a team using Python. The function maximumPerformance accepts parameters such as the number of engineers, lists of speeds, efficiencies, and a team size limit, then returns the maximum possible performance under given constraints.

How the solution works:

  1. Define a constatnt MOD to handle large numbers ensuring the result remains manageable and fits within typical limits by using a modulo operation.
  2. Pair each engineer's efficiency with their respective speed, resulting in a list of tuples.
  3. Sort this list in descending order based on efficiencies to prioritize adding the most efficient engineers to the team.
  4. Initialize a min-heap to keep track of the smallest speeds within the current team composition which aids in maximizing the team's performance.
  5. Establish variables for tracking current speed sums and the maximum performance discovered.
  6. Iterate through sorted engineer data:
    • Adjust the speed sum by removing the smallest speed if the current team size exceeds the limit.
    • Add the new engineer's speed to the min-heap and update the current speed sum.
    • Calculate the potential maximum performance by multiplying the current speed sum by the engineer's efficiency and check if it's the greatest found so far.
  7. Finally, return the best performance modulo MOD to get the result under the defined constraint, ensuring numerical stability.

The careful utilization of a priority queue (min-heap) to manage team size and composition effectively, combined with sorting by efficiencies, ensures that the algorithm remains efficient and can handle the given problem constraints effectively. This method ensures optimal performance calculation by keeping the most beneficial engineers in the team while respecting the team limit and efficiency factors.

Comments

No comments yet.