• 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
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
    【Spring】Spring(IoC、AOP、Bean生命周期、事务、三级缓存、源码)面试题
    ETLCloud助力富勒TMS实现物流数仓同步
    希尔排序:优化插入排序的精妙算法
    App加固中的代码混淆功能,让逆向工程师很头疼
    光模块厂家如何提高千兆光模块和万兆光模块的可靠性
    Vue的生命周期详解
    JavaCV音视频开发宝典:UDP推流 使用UDP方式推送TS流 实现UDP一对一直播点播
    Spring Boot集成Redis集群报错UnsupportedOperationException
    k8s驱逐(3)-kubelet节点压力驱逐-源码分析篇
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126803793