This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathipo.go
executable file
·90 lines (72 loc) · 1.68 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package problem0502
import (
"container/heap"
"sort"
)
func findMaximizedCapital(k int, W int, Profits []int, Capital []int) int {
size := len(Profits)
// caps 按照 capital 的升序排列 project
caps := make(capQueue, size)
for i := range Profits {
p := &project{
profit: Profits[i],
capital: Capital[i],
}
caps[i] = p
}
sort.Sort(caps)
// pros 是高 profit 优先的队列
pros := make(proPQ, 0, size)
var i int
for {
// 把所有可以做的 project 存入 pros
for i < len(caps) && caps[i].capital <= W {
heap.Push(&pros, caps[i])
i++
}
// 无 project 可做了
// 或
// 已经达到工作量的上限
if len(pros) == 0 || k == 0 {
break
}
// 从 pros 中挑选一件 profit 最大的工作来做
// 并更新 W
W += heap.Pop(&pros).(*project).profit
k--
}
return W
}
// entry 是 priorityQueue 中的元素
type project struct {
profit, capital int
}
// PQ implements heap.Interface and holds entries.
type capQueue []*project
func (q capQueue) Len() int { return len(q) }
func (q capQueue) Less(i, j int) bool {
return q[i].capital < q[j].capital
}
func (q capQueue) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
}
// PQ implements heap.Interface and holds entries.
type proPQ []*project
func (pq proPQ) Len() int { return len(pq) }
func (pq proPQ) Less(i, j int) bool {
return pq[i].profit > pq[j].profit
}
func (pq proPQ) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
// Push 往 pq 中放 entry
func (pq *proPQ) Push(x interface{}) {
entry := x.(*project)
*pq = append(*pq, entry)
}
// Pop 从 pq 中取出最优先的 entry
func (pq *proPQ) Pop() interface{} {
temp := (*pq)[len(*pq)-1]
*pq = (*pq)[:len(*pq)-1]
return temp
}