题目:原题链接(困难)
标签:贪心算法、排序、堆
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N l o g N ) O(NlogN) O(NlogN) | O ( N ) O(N) O(N) | 284ms (46.23%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
MOD = 10 ** 9 + 7
class Solution:
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
people = [[efficiency[i], speed[i]] for i in range(n)]
people.sort(reverse=True)
ans = 0
heap = []
now = 0
for e, s in people:
heapq.heappush(heap, s)
now += s
if len(heap) > k:
now -= heapq.heappop(heap)
ans = max(ans, now * e)
return ans % MOD