forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
30 lines (28 loc) · 875 Bytes
/
Solution.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
func getOrder(tasks [][]int) (ans []int) {
for i := range tasks {
tasks[i] = append(tasks[i], i)
}
sort.Slice(tasks, func(i, j int) bool { return tasks[i][0] < tasks[j][0] })
q := hp{}
i, t, n := 0, 0, len(tasks)
for len(q) > 0 || i < n {
if len(q) == 0 {
t = max(t, tasks[i][0])
}
for i < n && tasks[i][0] <= t {
heap.Push(&q, pair{tasks[i][1], tasks[i][2]})
i++
}
p := heap.Pop(&q).(pair)
ans = append(ans, p.i)
t += p.t
}
return
}
type pair struct{ t, i int }
type hp []pair
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any) { *h = append(*h, v.(pair)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }