355.design-twitter.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. type Twitter struct {
  2. tweets map[int]*list.List
  3. follow map[int]map[int]bool
  4. ts int
  5. }
  6. type tweet struct {
  7. id int
  8. ts int
  9. }
  10. type tweetPQ []*list.Element
  11. func (pq tweetPQ) Len() int { return len(pq) }
  12. func (pq tweetPQ) Less(i, j int) bool { return pq[j].Value.(tweet).ts < pq[i].Value.(tweet).ts }
  13. func (pq tweetPQ) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
  14. func (pq *tweetPQ) Push(x interface{}) {
  15. *pq = append(*pq, x.(*list.Element))
  16. }
  17. func (pq *tweetPQ) Pop() interface{} {
  18. x := (*pq)[pq.Len()-1]
  19. *pq = (*pq)[:pq.Len()-1]
  20. return x
  21. }
  22. /** Initialize your data structure here. */
  23. func Constructor() (twitter Twitter) {
  24. twitter.tweets = make(map[int]*list.List)
  25. twitter.follow = make(map[int]map[int]bool)
  26. return
  27. }
  28. /** Compose a new tweet. */
  29. func (this *Twitter) PostTweet(userId int, tweetId int) {
  30. if li, ok := this.tweets[userId]; ok {
  31. li.PushFront(tweet{tweetId, this.ts})
  32. } else {
  33. if _, ok := this.follow[userId]; !ok {
  34. this.follow[userId] = map[int]bool{userId: true}
  35. }
  36. li = list.New()
  37. li.PushFront(tweet{tweetId, this.ts})
  38. this.tweets[userId] = li
  39. }
  40. this.ts++
  41. }
  42. /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
  43. func (this *Twitter) GetNewsFeed(userId int) []int {
  44. feed := make([]int, 0)
  45. if user, ok := this.follow[userId]; ok {
  46. var pq tweetPQ
  47. for uid, _ := range user {
  48. if li, ok := this.tweets[uid]; ok {
  49. heap.Push(&pq, li.Front())
  50. }
  51. }
  52. for pq.Len() != 0 {
  53. t := heap.Pop(&pq).(*list.Element)
  54. feed = append(feed, t.Value.(tweet).id)
  55. if len(feed) == 10 {
  56. return feed
  57. }
  58. t = t.Next()
  59. if t != nil {
  60. heap.Push(&pq, t)
  61. }
  62. }
  63. }
  64. return feed
  65. }
  66. /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
  67. func (this *Twitter) Follow(followerId int, followeeId int) {
  68. if user, ok := this.follow[followerId]; !ok {
  69. this.follow[followerId] = map[int]bool{followerId: true, followeeId: true}
  70. } else {
  71. user[followeeId] = true
  72. }
  73. }
  74. /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
  75. func (this *Twitter) Unfollow(followerId int, followeeId int) {
  76. if user, ok := this.follow[followerId]; ok && followerId != followeeId {
  77. delete(user, followeeId)
  78. }
  79. }
  80. /**
  81. * Your Twitter object will be instantiated and called as such:
  82. * obj := Constructor();
  83. * obj.PostTweet(userId,tweetId);
  84. * param_2 := obj.GetNewsFeed(userId);
  85. * obj.Follow(followerId,followeeId);
  86. * obj.Unfollow(followerId,followeeId);
  87. */