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 mostk 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 modulo109 + 7.
Input: n =6, speed =[2,10,3,1,5,8], efficiency =[5,4,3,9,7,2], k =2Output: 60Explanation:
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:
1
2
3
4
Input: n =6, speed =[2,10,3,1,5,8], efficiency =[5,4,3,9,7,2], k =3Output: 68Explanation:
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:
1
2
Input: n =6, speed =[2,10,3,1,5,8], efficiency =[5,4,3,9,7,2], k =4Output: 72
To maximize performance, we want the largest possible sum of speeds, but the team’s minimum efficiency acts as a bottleneck. By sorting engineers by efficiency (from high to low), we can always ensure the current engineer’s efficiency is the minimum in the current team. We use a min-heap to keep track of the top k speeds.
classSolution {
publicintmaxPerformance(int n, int[] speed, int[] efficiency, int k) {
int MOD = (int)1e9 + 7;
int[][] eng =newint[n][2];
for (int i = 0; i < n; i++) {
eng[i][0]= efficiency[i];
eng[i][1]= speed[i];
}
Arrays.sort(eng, (a, b) -> b[0]- a[0]);
PriorityQueue<Integer> heap =new PriorityQueue<>();
long sSum = 0, ans = 0;
for (int[] e : eng) {
heap.offer(e[1]);
sSum += e[1];
if (heap.size() > k) sSum -= heap.poll();
ans = Math.max(ans, sSum * e[0]);
}
return (int)(ans % MOD);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funmaxPerformance(n: Int, speed: IntArray, efficiency: IntArray, k: Int): Int {
val MOD = 1_000_000_007
val eng = efficiency.zip(speed).sortedByDescending { it.first }
val heap = java.util.PriorityQueue<Int>()
var sSum = 0Lvar ans = 0Lfor ((e, s) in eng) {
heap.add(s)
sSum += s
if (heap.size > k) sSum -= heap.poll()
ans = maxOf(ans, sSum * e)
}
return (ans % MOD).toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defmaxPerformance(self, n: int, speed: list[int], efficiency: list[int], k: int) -> int:
MOD =10**9+7 eng = sorted(zip(efficiency, speed), reverse=True)
heap = []
s_sum =0 ans =0import heapq
for e, s in eng:
heapq.heappush(heap, s)
s_sum += s
if len(heap) > k:
s_sum -= heapq.heappop(heap)
ans = max(ans, s_sum * e)
return ans % MOD