-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook2.go
101 lines (88 loc) · 1.76 KB
/
facebook2.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
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"container/heap"
"log"
)
type ListNode struct {
Val int
Next *ListNode
}
func main() {
var lists []*ListNode
for _, list := range [][]int{[]int{1, 4, 5}, []int{1, 3, 4}, []int{2, 6}} {
lists = append(lists, List(list))
}
printList(mergeKLists(lists))
}
func List(list []int) *ListNode {
i := len(list) - 1
var current *ListNode
for i >= 0 {
item := ListNode{Val: list[i], Next: current}
log.Println(item)
current = &item
i--
}
return current
}
func printList(node *ListNode) {
for node != nil {
log.Println("NN", node, node.Val)
node = node.Next
}
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeKLists(lists []*ListNode) *ListNode {
pq := PriorityQueue{}
for _, current := range lists {
for current != nil {
item := Item{value: current.Val, priority: current.Val}
pq = append(pq, &item)
current = current.Next
}
}
heap.Init(&pq)
var current *ListNode
for pq.Len() > 0 {
item := heap.Pop(&pq).(*Item)
ln := ListNode{Val: item.value, Next: current}
current = &ln
}
return current
}
type Item struct {
value int
priority int
index int
}
type PriorityQueue []*Item
func (pq *PriorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*Item)
item.index = n
*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
return pq[i].priority > pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = j
pq[j].index = i
}