LeetCode 1383. Maximum Performance Of a Team
Question
There are n
engineers numbered from 1 to n
and two arrays: speed
and efficiency
, where speed[i]
and efficiency[i]
represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k
engineers, since the answer can be a huge number, return this modulo 10^9 + 7.
The performance of a team is the sum of their engineers’ speeds multiplied by the minimum efficiency among their engineers.
Example
1 | Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 |
Solution
以下主要参考lee215大佬的贪心解法。
首先,这里的计算方式为所有的速度之和乘以最小的效率值,即为sum(speed) * min(efficiency)
。并且说定了最多选k个,其实达到最高的计算值只由一个人完成(如同例子3)。我们需要保持最大的有效值(efficiency),利用最小堆保持速度,当选中的worker超过k就poll出来。其实这里为啥可以用,目前我也是懵逼。
Code
1 | // sort: nlogn, pq: nlogk, space: O(2 * n) |