设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,
能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能:
postTweet(userId, tweetId): 创建一条新的推文
getNewsFeed(userId): 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。
推文必须按照时间顺序由最近的开始排序。
follow(followerId, followeeId): 关注一个用户
unfollow(followerId, followeeId): 取消关注一个用户
示例:Twitter twitter = new Twitter();
// 用户1发送了一条新推文 (用户id = 1, 推文id = 5).
twitter.postTweet(1, 5);
// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
twitter.getNewsFeed(1);
// 用户1关注了用户2.
twitter.follow(1, 2);
// 用户2发送了一个新推文 (推文id = 6).
twitter.postTweet(2, 6);
// 用户1的获取推文应当返回一个列表,其中包含两个推文,id分别为 -> [6, 5].
// 推文id6应当在推文id5之前,因为它是在5之后发送的.
twitter.getNewsFeed(1);
// 用户1取消关注了用户2.
twitter.unfollow(1, 2);
// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
// 因为用户1已经不再关注用户2.
twitter.getNewsFeed(1);
1、暴力法;时间复杂度O(n^2),空间复杂度O(n)
type Twitter struct {
data [][2]int
m map[int]map[int]int
}
func Constructor() Twitter {
return Twitter{
data: make([][2]int, 0),
m: make(map[int]map[int]int),
}
}
func (this *Twitter) PostTweet(userId int, tweetId int) {
this.data = append(this.data, [2]int{userId, tweetId})
}
func (this *Twitter) GetNewsFeed(userId int) []int {
res := make([]int, 0)
for i := len(this.data) - 1; i >= 0; i-- { // 遍历发表的列表
id, tid := this.data[i][0], this.data[i][1]
if id == userId || this.m[userId][id] > 0 {
res = append(res, tid)
}
if len(res) == 10 {
return res
}
}
return res
}
func (this *Twitter) Follow(followerId int, followeeId int) {
if this.m[followerId] == nil {
this.m[followerId] = make(map[int]int)
}
this.m[followerId][followeeId] = 1
}
func (this *Twitter) Unfollow(followerId int, followeeId int) {
if this.m[followerId] != nil {
this.m[followerId][followeeId] = 0
}
}
2、堆;时间复杂度O(nlog(n)),空间复杂度O(n)
type Twitter struct {
data map[int][][2]int
m map[int]map[int]int
count int
}
func Constructor() Twitter {
return Twitter{
data: make(map[int][][2]int),
m: make(map[int]map[int]int),
count: 0,
}
}
func (this *Twitter) PostTweet(userId int, tweetId int) {
this.data[userId] = append(this.data[userId], [2]int{this.count, tweetId})
this.count++
}
func (this *Twitter) GetNewsFeed(userId int) []int {
res := make([]int, 0)
intHeap := make(IntHeap, 0)
heap.Init(&intHeap)
for i := len(this.data[userId]) - 1; i >= 0; i-- {
a, b := this.data[userId][i][0], this.data[userId][i][1]
if intHeap.Len() == 10 && intHeap[0][0] > a {
break
}
heap.Push(&intHeap, [2]int{a, b})
if intHeap.Len() > 10 {
heap.Pop(&intHeap)
}
}
for k, v := range this.m[userId] {
if k == userId || v == 0 {
continue
}
for i := len(this.data[k]) - 1; i >= 0; i-- {
a, b := this.data[k][i][0], this.data[k][i][1]
if intHeap.Len() == 10 && intHeap[0][0] > a {
break
}
heap.Push(&intHeap, [2]int{a, b})
if intHeap.Len() > 10 {
heap.Pop(&intHeap)
}
}
}
for intHeap.Len() > 0 {
node := heap.Pop(&intHeap).([2]int)
res = append([]int{node[1]}, res...)
}
return res
}
func (this *Twitter) Follow(followerId int, followeeId int) {
if this.m[followerId] == nil {
this.m[followerId] = make(map[int]int)
}
this.m[followerId][followeeId] = 1
}
func (this *Twitter) Unfollow(followerId int, followeeId int) {
if this.m[followerId] != nil {
this.m[followerId][followeeId] = 0
}
}
type IntHeap [][2]int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i][0] < h[j][0] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.([2]int)) }
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
Medium题目,设计题目
页面更新:2024-03-12
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号