Maximum Performance of a Team — LeetCode 1383 Python Solution
HardGreedyArraySortingHeap (Priority Queue)
- Problem
- #1383
- Pattern
- Heap / Priority Queue
- Reading time
- 3 min
- Source
- leetcode.com
The problem
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 n.
Example
- 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.
Python solution
Python
class Solution:
def maxPerformance(
self, n: int, speed: List[int], efficiency: List[int], k: int
) -> int:
t = sorted(zip(speed, efficiency), key=lambda x: -x[1])
ans = tot = 0
mod = 10**9 + 7
h = []
for s, e in t:
tot += s
ans = max(ans, tot * e)
heappush(h, s)
if len(h) == k:
tot -= heappop(h)
return ans % modComplexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) |
| Space | O(1) to O(n) auxiliary |
Pattern: Heap / Priority Queue
Keep only the best k elements, or always pull the smallest, in log time. LeetCode 1383. Maximum Performance of a Team is filed here because LeetCode tags it Heap (Priority Queue), which is the vocabulary this hub collects.
The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1383. Maximum Performance of a Team?
- LeetCode 1383. Maximum Performance of a Team is rated Hard on LeetCode.
- What topics does LeetCode 1383. Maximum Performance of a Team cover?
- LeetCode 1383. Maximum Performance of a Team is tagged Greedy, Array, Sorting and Heap (Priority Queue) on LeetCode.