forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0355-design-twitter.go
87 lines (75 loc) · 2.56 KB
/
0355-design-twitter.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
type Tweet struct {
count int
tweetId int
followeeId int
index int
}
type TweetHeap []*Tweet
func (h TweetHeap) Len() int { return len(h) }
func (h TweetHeap) Less(i, j int) bool { return h[i].count < h[j].count }
func (h TweetHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *TweetHeap) Push(x interface{}) {*h = append(*h, x.(*Tweet))}
func (h *TweetHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
type Twitter struct {
count int
tweetMap map[int][]*Tweet
followMap map[int]map[int]bool
}
func Constructor() Twitter {
return Twitter{tweetMap: make(map[int][]*Tweet), followMap: make(map[int]map[int]bool)}
}
func (this *Twitter) PostTweet(userId int, tweetId int) {
if _, ok := this.tweetMap[userId]; !ok {
this.tweetMap[userId] = make([]*Tweet, 0)
}
this.tweetMap[userId] = append(this.tweetMap[userId], &Tweet{count: this.count, tweetId: tweetId})
this.count -= 1
}
func (this *Twitter) GetNewsFeed(userId int) []int {
res := make([]int, 0)
minHeap := TweetHeap{}
if _, ok := this.followMap[userId]; !ok {
this.followMap[userId] = make(map[int]bool)
}
this.followMap[userId][userId] = true;
for followeeId, _ := range this.followMap[userId] {
if _, ok := this.tweetMap[followeeId]; ok {
index := len(this.tweetMap[followeeId]) - 1
tweet := this.tweetMap[followeeId][index]
heap.Push(&minHeap, &Tweet{
count: tweet.count,
tweetId: tweet.tweetId,
followeeId: followeeId,
index: index - 1})
}
}
for len(minHeap) > 0 && len(res) < 10 {
tweet := heap.Pop(&minHeap).(*Tweet)
res = append(res, tweet.tweetId)
if tweet.index >= 0 {
nextTweet := this.tweetMap[tweet.followeeId][tweet.index]
heap.Push(&minHeap, &Tweet{count: nextTweet.count,
tweetId: nextTweet.tweetId,
followeeId: tweet.followeeId,
index: tweet.index - 1})
}
}
return res
}
func (this *Twitter) Follow(followerId int, followeeId int) {
if _, ok := this.followMap[followerId]; !ok {
this.followMap[followerId] = make(map[int]bool)
}
this.followMap[followerId][followeeId] = true
}
func (this *Twitter) Unfollow(followerId int, followeeId int) {
if _, ok := this.followMap[followerId]; ok {
delete(this.followMap[followerId], followeeId)
}
}