#1383
Maximum Performance of a Team
HardArrayGreedySortingHeap (Priority Queue)GreedyHeapSorting
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n log n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n log n)Space O(n)
By sorting engineers based on efficiency and using a min-heap to keep track of the top speeds, we can efficiently calculate the maximum performance without checking all combinations.
⚙️
Algorithm
4 steps- 1Step 1: Pair each engineer's speed with their efficiency and sort these pairs by efficiency in descending order.
- 2Step 2: Use a min-heap to keep track of the top k speeds as we iterate through the sorted list.
- 3Step 3: For each engineer, calculate the current performance using the minimum efficiency (current engineer's efficiency) and the sum of speeds in the heap.
- 4Step 4: Update the maximum performance found.
solution.py15 lines
1# Full working Python code
2import heapq
3
4def maxPerformance(n, speed, efficiency, k):
5 engineers = sorted(zip(efficiency, speed), reverse=True)
6 max_perf = 0
7 speed_sum = 0
8 min_heap = []
9 for eff, spd in engineers:
10 heapq.heappush(min_heap, spd)
11 speed_sum += spd
12 if len(min_heap) > k:
13 speed_sum -= heapq.heappop(min_heap)
14 max_perf = max(max_perf, speed_sum * eff)
15 return max_perf % (10**9 + 7)ℹ
Complexity note: The sorting step takes O(n log n), and maintaining the min-heap takes O(n) in total for all engineers, leading to an overall complexity of O(n log n).
- 1Sorting engineers by efficiency helps in maximizing performance.
- 2Using a min-heap allows efficient management of the top speeds.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.