Skip to content

1383. Maximum Performance of a Team

Description

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

 

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

 

Solutions

Solution: Priority Queue

  • Time complexity: O(nlogn + nlogk)
  • Space complexity: O(n)

 

JavaScript

js
/**
 * @param {number} n
 * @param {number[]} speed
 * @param {number[]} efficiency
 * @param {number} k
 * @return {number}
 */
const maxPerformance = function (n, speed, efficiency, k) {
  const MODULO = BigInt(10 ** 9 + 7);
  const engineers = [];
  const speedQueue = new MinPriorityQueue();
  let totalSpeed = 0n;
  let result = 0n;

  for (let index = 0; index < n; index++) {
    engineers.push({ speed: speed[index], efficiency: efficiency[index] });
  }

  engineers.sort((a, b) => b.efficiency - a.efficiency);

  for (const engineer of engineers) {
    totalSpeed += BigInt(engineer.speed);
    speedQueue.enqueue(engineer.speed);

    if (speedQueue.size() > k) {
      totalSpeed -= BigInt(speedQueue.dequeue().element);
    }
    const performance = totalSpeed * BigInt(engineer.efficiency);

    if (performance <= result) continue;

    result = performance;
  }
  return Number(result % MODULO);
};

Released under the MIT license