forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1383.go
54 lines (45 loc) · 1.08 KB
/
1383.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
type IntHeap []int64
func (h IntHeap) Len() int {
return len(h)
}
func (h IntHeap) Less(i, j int) bool {
return h[i] < h[j]
}
func (h IntHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *IntHeap) Pop() interface{} {
x := (*h)[(*h).Len()-1]
*h = (*h)[:(*h).Len()-1]
return x
}
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int64))
}
func maxPerformance(n int, s []int, e []int, k int) int {
engineers := make([][]int64, n)
for i := 0; i < n; i++ {
engineers[i] = []int64{int64(e[i]), int64(s[i])}
}
sort.Slice(engineers, func(i, j int) bool {
return engineers[i][0] > engineers[j][0]
})
var res, sSum int64
res, sSum = 0, 0
pq := new(IntHeap)
for _, engineer := range engineers {
sSum += engineer[1]
res = max(res, sSum * engineer[0])
heap.Push(pq, engineer[1])
if len(*pq) >= k {
sSum -= heap.Pop(pq).(int64)
}
}
return int(res % 1000000007)
}
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}