LeetCode 1383. Maximum Performance of a Team

原题链接在这里: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.

posted @ 2024-08-13 23:43  Dylan_Java_NYC  阅读(22)  评论(0编辑  收藏  举报