-
Notifications
You must be signed in to change notification settings - Fork 1
/
pooling.go
77 lines (66 loc) · 1.47 KB
/
pooling.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
package pooling
import (
"container/heap"
"sort"
)
func carPooling(trips [][]int, capacity int) bool {
// return useBucket(trips, capacity)
return usePriorityQueue(trips, capacity)
}
// useBucket time complexity O(N+1001), space complexity O(1)
func useBucket(trips [][]int, capacity int) bool {
stops := [1001]int{}
for _, trip := range trips {
p, s, e := trip[0], trip[1], trip[2]
stops[s] += p
stops[e] -= p
}
for i := 0; i < 1001 && capacity > 0; i++ {
capacity -= stops[i]
}
return capacity >= 0
}
// usePriorityQueue time complexity O(nLgN) space complexity O(N)
func usePriorityQueue(trips [][]int, capacity int) bool {
sort.Slice(trips, func(i, j int) bool {
return trips[i][1] < trips[j][1]
})
pq := make(lpq, 0, len(trips))
for _, trip := range trips {
tn, s := trip[0], trip[1]
for pq.Len() > 0 && pq.Top().([]int)[2] <= s {
item := heap.Pop(&pq).([]int)
capacity += item[0]
}
capacity -= tn
if capacity < 0 {
return false
}
heap.Push(&pq, trip)
}
return true
}
type lpq [][]int
func (pq lpq) Len() int { return len(pq) }
func (pq lpq) Less(i, j int) bool {
return pq[i][2] < pq[j][2]
}
func (pq lpq) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *lpq) Push(x interface{}) {
item := x.([]int)
*pq = append((*pq), item)
}
func (pq *lpq) Top() interface{} {
// n := len(*pq)
return (*pq)[0]
}
func (pq *lpq) Pop() interface{} {
old := *pq
n := len(old)
item := old[0]
old[0] = nil
*pq = old[1:n]
return item
}