-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipo.go
58 lines (51 loc) · 1.06 KB
/
ipo.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
55
56
57
58
package main
import (
"container/heap"
"sort"
)
// 502 https://leetcode-cn.com/problems/ipo/
// 根堆 + 贪心
type node struct {
profit, need int
}
type hp []node
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool {
if h[i].profit > h[j].profit {
return true
}
return false
}
func (h hp) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *hp) Pop() (v interface{}) {
v, *h = (*h)[len(*h)-1], (*h)[:len(*h)-1]
return
}
func (h *hp) Push(v interface{}) {
*h = append(*h, v.(node))
}
func findMaximizedCapital(k int, w int, profits []int, capital []int) int {
n := make([]node, 0)
for i := 0; i < len(profits); i++ {
n = append(n, node{profit: profits[i], need: capital[i]})
}
sort.Slice(n, func(i, j int) bool {
if n[i].need < n[j].need {
return true
}
return false
})
h := make(hp, 0)
for idx := 0; k > 0; k-- {
for idx < len(n) && w >= n[idx].need {
heap.Push(&h, n[idx])
idx++
}
if h.Len() != 0 {
w += heap.Pop(&h).(node).profit
}
}
return w
}