
😎풀이
- 트윗을 담당할
posts
선언
- 팔로우 상태를 연결할
followStatus
선언
postTweet
할 경우 게시물 배열에 추가하고 내가 조회할 수 있게 내 팔로잉 목록에 자신 추가
getNewsFeed
할 경우 게시물을 역순으로 조회하며 내가 팔로우 하는 회원의 글 인 경우 뉴스피드에 추가
follow
할 경우 내 follow
목록에 추가
unfollow
할 경우 내 follow
목록에서 제거
class Twitter {
private posts: number[][]
private followStatus: Map<number, Set<number>>
constructor() {
this.posts = []
this.followStatus = new Map<number, Set<number>>()
}
postTweet(userId: number, tweetId: number): void {
this.posts.push([userId, tweetId])
if(!this.followStatus.has(userId)) this.followStatus.set(userId, new Set([userId]))
}
getNewsFeed(userId: number): number[] {
const myFollowers = this.followStatus.get(userId)
if(!myFollowers) return []
const newsFeeds = []
for(let i = this.posts.length - 1; i >= 0; i--) {
if(newsFeeds.length >= 10) return newsFeeds
const [authorId, postId] = this.posts[i]
if(!myFollowers.has(authorId)) continue
newsFeeds.push(postId)
}
return newsFeeds
}
follow(followerId: number, followeeId: number): void {
if(!this.followStatus.has(followerId)) this.followStatus.set(followerId, new Set([followerId]))
const followers = this.followStatus.get(followerId)
followers.add(followeeId)
}
unfollow(followerId: number, followeeId: number): void {
if(!this.followStatus.has(followerId)) return
const followers = this.followStatus.get(followerId)
followers.delete(followeeId)
}
}