LeetCode每日一题(1383. Maximum Performance of a Team)

给定n名工程师的速度和效率,以及最多选择k名工程师组成团队,团队性能为工程师速度之和乘以最低效率。目标是求解最大团队性能。通过选择效率排序后的工程师并计算每名工程师与其他k-1名速度最快工程师组合的性能,找到最大性能并返回模10^9+7的结果。

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

提交过一次才发现这题以前已经做过了, 但是没什么印象了, 不过令自己欣慰的是遇到这种题已经不会再像以前那样毫无头绪了, 可以自己把它解出来了。

拿到题首先想到的就是排序,但是是按 speed 排序还是按 efficiency 排序呢?因为 speed 是 sum, 而我们要找出 min(efficiencies)作为乘数, 所以我们肯定选择的是按 efficiency 进行排序。拿到已经排序的 engineers 后, 这个题就变成了对于每个 engineers[i], 我们要找到 engineers[i]之后的 k-1 个 speed 最大的 engineers, 因为只要 engineers[i]之后的 engineers 的 efficiency 一定是大于 engineers[i].efficiency 的, 所以此时的 min(efficiency)一定是 engineers[i].efficiency, 而 speed 的 sum 等于 engineers[i].speed 加上后面 k-1 个 speed 最大的 engineers 的 speed 的和。这样我们就可以算出当包含 engineers[i]时的最大 performance, 然后我们从中挑出最大的 performance 就可以了。不过这里有一点要注意, 就是如果 performance%1000000007 之后再进行大小的对比,结果肯定是不准的,所以只能是最后结果的时候再进行 mod 操作



impl Solution {
    pub fn max_performance(n: i32, speed: Vec<i32>, efficiency: Vec<i32>, k: i32) -> i32 {
        let mut engineers: Vec<(i128, i128)> = speed
            .into_iter()
            .zip(efficiency)
            .map(|(s, e)| (e as i128, s as i128))
            .collect();
        engineers.sort();
        let mut heap = BinaryHeap::new();
        let mut sum = 0;
        let mut ans = 0;
        for i in (0..engineers.len()).rev() {
            ans = ans.max((engineers[i].1 + sum) * engineers[i].0);
            heap.push(Reverse(engineers[i].1));
            sum += engineers[i].1;
            if heap.len() == k as usize {
                let Reverse(head) = heap.pop().unwrap();
                sum -= head;
            }
        }
        (ans % (10i128.pow(9) + 7)) as i32
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值