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

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


版权声明:本文为wangjun861205原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。