LeetCode 1383 Maximum Performance of a Team (优先队列 贪心 推荐)

60 篇文章 0 订阅
34 篇文章 3 订阅

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 nspeed[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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值