【leetcode】1383. Maximum Performance of a Team

百般思念 提交于 2020-03-29 07:33:51

题目如下:

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
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 <= n <= 10^5
  • speed.length == n
  • efficiency.length == n
  • 1 <= speed[i] <= 10^5
  • 1 <= efficiency[i] <= 10^8
  • 1 <= k <= n

解题思路:本题的关键是找出满足条件的k个工程师中效率最低的那个,确定了效率最低的工程师作为基准点后,接下来计算比这个人效率高的工程师中速度的最大值之和即可。

代码如下:

class Solution(object):
    def maxPerformance(self, n, speed, efficiency, k):
        """
        :type n: int
        :type speed: List[int]
        :type efficiency: List[int]
        :type k: int
        :rtype: int
        """
        pair = []
        for s,e in zip(speed,efficiency):
            pair.append((s,e))

        def cmpf(v1,v2):
            if v1[1] != v2[1]:
                return v1[1] - v2[1]
            return v2[0] - v1[0]
        pair.sort(cmp=cmpf)
        import bisect

        res = 0
        total = None
        speed.sort()
        for i in range(len(pair)):
            inx = bisect.bisect_left(speed,pair[i][0])
            flag = (inx + k - 1) >= len(speed)
            del speed[inx]
            if total == None:
                total = sum(speed[len(speed)-k + 1:len(speed)])
            elif i + k > len(pair):
                total -= pair[i][0]
            elif flag:
                total -= pair[i][0]
                total += speed[len(speed) - k + 1]

            res = max(res,(total+ pair[i][0]) * pair[i][1])
            #del speed[inx]

        return res % (10**9 + 7)

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!