##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1383.html

# 1383. Maximum Performance of a Team (Hard)

There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7.

The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.

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

Related Topics:
Greedy, Sort

## Solution 1. Greedy

### Intuition

For a given efficiency, we pick all works with the same or better efficiency. If the number of workers is greater than k, we pick k fastest workers.

### Algorithm

We greedily try each engineer from the most efficient one to the least one.

For each engineer:

• first try adding him to the team and add his speed[i] to the sum.
• If after the addition there are more than k engineers in the team, pop the one with the least speed, and deduct his speed from sum.
• try to update the answer using sum * speed[i].

The idea behind

// OJ: https://leetcode.com/problems/maximum-performance-of-a-team/
// Time: O(NlogN)
// Space: O(N)
// Ref: https://leetcode.com/problems/maximum-performance-of-a-team/discuss/539687/JavaC%2B%2BPython-Priority-Queue
class Solution {
public:
int maxPerformance(int N, vector<int>& S, vector<int>& E, int K) {
vector<pair<int, int>> ps(N);
for (int i = 0; i < N; ++i) ps[i] = {E[i], S[i]};
sort(ps.begin(), ps.end());
long sum = 0, ans = 0;
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = N - 1; i >= 0; --i) {
pq.push(ps[i].second);
sum += ps[i].second;
if (pq.size() > K) {
sum -= pq.top();
pq.pop();
}
ans = max(ans, sum * ps[i].first);
}
return ans % (int)(1e9+7);
}
};

• class Solution {
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
final int MODULO = 1000000007;
int[][] speedEfficiencyArray = new int[n];
for (int i = 0; i < n; i++) {
speedEfficiencyArray[i] = speed[i];
speedEfficiencyArray[i] = efficiency[i];
}
Arrays.sort(speedEfficiencyArray, new Comparator<int[]>() {
public int compare(int[] speedEfficiency1, int[] speedEfficiency2) {
if (speedEfficiency1 != speedEfficiency2)
return speedEfficiency2 - speedEfficiency1;
else
return speedEfficiency2 - speedEfficiency1;
}
});
long maxPerformance = 0;
PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
long speedSum = 0;
int minEfficiency = Integer.MAX_VALUE;
for (int i = 0; i < k; i++) {
int[] speedEfficiency = speedEfficiencyArray[i];
int curSpeed = speedEfficiency;
int curEfficiency = speedEfficiency;
priorityQueue.offer(curSpeed);
speedSum += curSpeed;
minEfficiency = Math.min(minEfficiency, curEfficiency);
long curPerformance = speedSum * (long) minEfficiency;
maxPerformance = Math.max(maxPerformance, curPerformance);
}
for (int i = k; i < n; i++) {
int prevSpeed = priorityQueue.poll();
speedSum -= prevSpeed;
int[] speedEfficiency = speedEfficiencyArray[i];
int curSpeed = speedEfficiency;
int curEfficiency = speedEfficiency;
priorityQueue.offer(curSpeed);
speedSum += curSpeed;
minEfficiency = Math.min(minEfficiency, curEfficiency);
long curPerformance = speedSum * (long) minEfficiency;
maxPerformance = Math.max(maxPerformance, curPerformance);
}
return (int) (maxPerformance % MODULO);
}
}

############

class Solution {
private static final int MOD = (int) 1e9 + 7;

public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
int[][] t = new int[n];
for (int i = 0; i < n; ++i) {
t[i] = new int[] {speed[i], efficiency[i]};
}
Arrays.sort(t, (a, b) -> b - a);
PriorityQueue<Integer> q = new PriorityQueue<>();
long tot = 0;
long ans = 0;
for (var x : t) {
int s = x, e = x;
tot += s;
ans = Math.max(ans, tot * e);
q.offer(s);
if (q.size() == k) {
tot -= q.poll();
}
}
return (int) (ans % MOD);
}
}

• // OJ: https://leetcode.com/problems/maximum-performance-of-a-team/
// Time: O(NlogN)
// Space: O(N)
// Ref: https://leetcode.com/problems/maximum-performance-of-a-team/discuss/539687/JavaC%2B%2BPython-Priority-Queue
class Solution {
public:
int maxPerformance(int N, vector<int>& S, vector<int>& E, int K) {
vector<pair<int, int>> ps(N);
for (int i = 0; i < N; ++i) ps[i] = {E[i], S[i]};
sort(ps.begin(), ps.end());
long sum = 0, ans = 0, mod = 1e9 + 7;
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = N - 1; i >= 0; --i) {
pq.push(ps[i].second);
sum += ps[i].second;
if (pq.size() > K) {
sum -= pq.top();
pq.pop();
}
ans = max(ans, sum * ps[i].first);
}
return ans % mod;
}
};

• class Solution:
def maxPerformance(
self, n: int, speed: List[int], efficiency: List[int], k: int
) -> int:
t = sorted(zip(speed, efficiency), key=lambda x: -x)
ans = tot = 0
mod = 10**9 + 7
h = []
for s, e in t:
tot += s
ans = max(ans, tot * e)
heappush(h, s)
if len(h) == k:
tot -= heappop(h)
return ans % mod


• func maxPerformance(n int, speed []int, efficiency []int, k int) int {
t := make([][]int, n)
for i, s := range speed {
t[i] = []int{s, efficiency[i]}
}
sort.Slice(t, func(i, j int) bool { return t[i] > t[j] })
var mod int = 1e9 + 7
ans, tot := 0, 0
pq := hp{}
for _, x := range t {
s, e := x, x
tot += s
ans = max(ans, tot*e)
heap.Push(&pq, s)
if pq.Len() == k {
tot -= heap.Pop(&pq).(int)
}
}
return ans % mod
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

type hp struct{ sort.IntSlice }

func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{} {
a := h.IntSlice
v := a[len(a)-1]
h.IntSlice = a[:len(a)-1]
return v
}
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }