Skip to content

Commit 61c4ed9

Browse files
committed
added a problem appeared in real interview
1 parent 4b2a34e commit 61c4ed9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

data_structure/heap.md

+28
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,34 @@ class Solution:
8686

8787
Heap 可以高效地取出或更新当前池中优先级最高的元素,因此适用于一些需要 greedy 算法的场景。
8888

89+
### [maximum-performance-of-a-team](https://leetcode-cn.com/problems/maximum-performance-of-a-team/)
90+
91+
> 公司有 n 个工程师,给两个数组 speed 和 efficiency,其中 speed[i] 和 efficiency[i] 分别代表第 i 位工程师的速度和效率。请你返回由最多 k 个工程师组成的团队的最大表现值。表现值的定义为:一个团队中所有工程师速度的和乘以他们效率值中的最小值。
92+
>
93+
94+
**图森面试真题**[See my review here.](https://leetcode.com/problems/maximum-performance-of-a-team/discuss/741822/Met-this-problem-in-my-interview!!!-(Python3-greedy-with-heap)) [或者这里(中文)](https://leetcode-cn.com/problems/maximum-performance-of-a-team/solution/greedy-with-min-heap-lai-zi-zhen-shi-mian-shi-de-j/)
95+
96+
```Python
97+
class Solution:
98+
def maxPerformance(self, n, speed, efficiency, k):
99+
100+
people = sorted(zip(speed, efficiency), key=lambda x: -x[1])
101+
102+
result, sum_speed = 0, 0
103+
min_heap = []
104+
105+
for i, (s, e) in enumerate(people):
106+
if i < k:
107+
sum_speed += s
108+
result = max(result, sum_speed * e)
109+
heapq.heappush(min_heap, s)
110+
elif s > min_heap[0]:
111+
sum_speed += s - heapq.heappushpop(min_heap, s)
112+
result = max(result, sum_speed * e)
113+
114+
return result #% 1000000007
115+
```
116+
89117
### [ipo](https://leetcode-cn.com/problems/ipo/)
90118

91119
**图森面试真题**。贪心策略为每次做当前成本范围内利润最大的项目。

0 commit comments

Comments
 (0)