首页 > 其他分享 >LeetCode 1383. Maximum Performance of a Team

LeetCode 1383. Maximum Performance of a Team

时间:2024-08-13 23:49:52浏览次数:10  
标签:int sum Team efficiency Performance performance team speed LeetCode

原题链接在这里:https://leetcode.com/problems/maximum-performance-of-a-team/description/

题目:

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 its engineers' speeds multiplied by the minimum efficiency among its 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

题解:

The question is ask to maximize sum(speed) * min(efficiency).

Then try every efficiency from high -> low, and meanwhile maintain the as larget as possible speed group. Keep adding spped to the total speed. 
If the group size == k, delete the smallest speed.

We could first sort array based on efficiency.

And use a minHeap to record the group speed to easily poll the smallest speed.

Time Complexity: O(nlogn).

Space: O(n).

AC Java:

 1 class Solution {
 2     public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
 3         if(speed == null || speed.length == 0 || speed.length != efficiency.length || k <= 0){
 4             return 0;
 5         }
 6 
 7         int mod = (int)1e9 + 7;
 8         int[][] arr = new int[n][2];
 9         for(int i = 0; i < n; i++){
10             arr[i] = new int[]{efficiency[i], speed[i]};
11         }
12 
13         Arrays.sort(arr, (a, b) -> b[0] - a[0]);
14         PriorityQueue<Integer> minHeap = new PriorityQueue<>();
15         long res = 0;
16         long sum = 0;
17         for(int [] a : arr){
18             if(minHeap.size() == k){
19                 sum -= minHeap.poll();
20             }
21 
22             sum += a[1];
23             minHeap.add(a[1]);
24             res = Math.max(res, sum * a[0]);
25         }
26 
27         return (int)(res % mod);
28     }
29 }

类似Minimum Cost to Hire K Workers.

标签:int,sum,Team,efficiency,Performance,performance,team,speed,LeetCode
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18357961

相关文章

  • Leetcode JAVA刷刷站(20)有效的括号
    一、题目概述二、思路方向     在Java中,要判断一个仅包含括号('(',')','{','}','[',']')的字符串是否有效,你可以使用栈(Stack)数据结构来实现。栈是一种后进先出(LIFO,LastInFirstOut)的数据结构,非常适合用来处理这类问题。以下是具体的实现步骤和代码示例:创......
  • LeetCode 1359. Count All Valid Pickup and Delivery Options
    原题链接在这里:https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/description/题目:Given n orders,eachorderconsistsofapickupandadeliveryservice.Countallvalidpickup/deliverypossiblesequencessuchthatdelivery(i)isalw......
  • WookTeam searchinfo SQL注入漏洞复现
    0x01产品简介WookTeam是一款轻量级的在线团队协作工具,提供各类文档工具、在线思维导图、在线流程图、项目管理、任务分发,知识库管理等工具。0x02漏洞概述WookTeam/api/users/searchinfo 接口存在SQL注入漏洞,未经身份验证的恶意攻击者利用SQL注入漏洞获取数据库中的信......
  • leetcode面试经典150题-122. 买卖股票的最佳时机 II
    https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150 gopackageleetcode150import"testing"funcTestMaxProfit2(t*testing.T){prices:=[]int{7,1,5,3,6,4}......
  • leetcode面试经典150题- 121. 买卖股票的最佳时机
    https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150gopackageleetcode150import("testing")funcTestMaxProfit(t*testing.T){prices:=[]int{2,1,2,0,1}......
  • leetcode面试经典150题- 189. 轮转数组
     https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150  gopackageleetcode150import"testing"funcTestRotate(t*testing.T){nums:=[]int{1,2}rotate2(nums,3)for_,num......
  • 代码随想录算法训练营第 42 天 |LeetCode 188.买卖股票的最佳时机IV LeetCode309.最佳
    代码随想录算法训练营Day42代码随想录算法训练营第42天|LeetCode188.买卖股票的最佳时机IVLeetCode309.最佳买卖股票时机含冷冻期LeetCode714.买卖股票的最佳时机含手续费目录代码随想录算法训练营前言LeetCode188.买卖股票的最佳时机IVLeetCode309.最佳买卖......
  • 推荐一款多功能Steam工具箱:Watt Toolkit
    WattToolkit工具箱(原名Steam++)是一个包含多种Steam工具功能的工具箱,此工具的大部分功能都是需要您下载安装Steam才能使用,工具预计将整合进大部分常用的Steam相关工具,并且尽力做到比原工具更好用,在各种整合添加功能的同时,也会注意体积尽量的控制到最小。功能介绍1.反代St......
  • 【学习笔记6】论文SQLfuse: Enhancing Text-to-SQL Performance through Comprehensiv
    Abstract        Text-to-SQL转换是一项关键创新,简化了从复杂SQL语句到直观自然语言查询的转换,尤其在SQL在各类岗位中广泛应用的情况下,这一创新显得尤为重要。随着GPT-3.5和GPT-4等大型语言模型(LLMs)的兴起,这一领域得到了极大的推动,提供了更好的自然语言理解......
  • 四数相加2 | LeetCode-454 | 哈希集合 | Java详细注释
    ......