type Tweet struct {
TweetId int
Num int
}
type TweetList []Tweet
func (t TweetList) Len() int { return len(t) }
func (t TweetList) Less(i, j int) bool { return t[i].Num < t[j].Num }
func (t TweetList) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
type Twitter struct {
Count int
NewsFeed map[int][]Tweet
FollowList map[int][]int
}
func Constructor() Twitter {
return Twitter{0, make(map[int][]Tweet), make(map[int][]int)}
}
func (this *Twitter) PostTweet(userId int, tweetId int) {
this.Count++
count := this.Count
this.NewsFeed[userId] = append(this.NewsFeed[userId], Tweet{tweetId, count})
}
func (this *Twitter) GetNewsFeed(userId int) []int {
var list []Tweet
var result []int
list = append(list, this.NewsFeed[userId]...)
for _, followee := range this.FollowList[userId] {
for _, tweet := range this.NewsFeed[followee] {
list = append(list, tweet)
}
}
sort.Sort(sort.Reverse(TweetList(list)))
for _, tweet := range list {
result = append(result, tweet.TweetId)
}
if len(result) > 10 {
return result[:10]
}
return result
}
func (this *Twitter) Follow(followerId int, followeeId int) {
var exist bool
if followerId != followeeId {
for _, followee := range this.FollowList[followerId] {
if followee == followeeId {
exist = true
break
}
}
if !exist {
this.FollowList[followerId] = append(this.FollowList[followerId], followeeId)
}
}
}
func (this *Twitter) Unfollow(followerId int, followeeId int) {
for index, followee := range this.FollowList[followerId] {
if followee == followeeId {
this.FollowList[followerId] = append(this.FollowList[followerId][:index], this.FollowList[followerId][index+1:]...)
}
}
}