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 their engineers' speeds multiplied by the minimum efficiency among their engineers.
Return?the maximum performance of this team. Since the answer can be a huge number, return it?modulo?10^9?+ 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 <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8
题目链接:https://leetcode.com/problems/maximum-performance-of-a-team/submissions/
题目大意:n个人,每人有个速度和效率值属性,最多从中选k人组成一个团队,团队的效率值为团队中每人速度之和与效率最小值的乘积,求可组成团队的效率的最大值
题目分析:按效率值从大到小排序,枚举效率值求以当前效率值为最小值时所能获得的团队效率最大值,因效率值已逆序排序,速度和必然是选当前所遍历的人里前k大的,当不满k个人时,就选当前枚举的所有人即可,因为要维护前k大,用优先队列维护比较方便
45ms,时间击败91.80%
class Solution {
class Engineer {
int s, e;
Engineer(int s, int e) {
this.s = s;
this.e = e;
}
}
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
Engineer[] es = new Engineer[n];
for (int i = 0; i < n; i++) {
es[i] = new Engineer(speed[i], efficiency[i]);
}
Arrays.sort(es, (e1, e2) -> (e2.e - e1.e));
PriorityQueue<Integer> pq = new PriorityQueue<>();
long sum = 0, ans = 0;
for (int i = 0; i < k; i++) {
sum += es[i].s;
pq.offer(es[i].s);
ans = Math.max(ans, sum * es[i].e);
}
for (int i = k; i < n; i++) {
int kthVal = pq.peek();
if (kthVal < es[i].s) {
sum -= kthVal;
sum += es[i].s;
pq.offer(es[i].s);
pq.poll();
ans = Math.max(ans, sum * es[i].e);
}
}
return (int)(ans % 1000000007);
}
}
|