using
System;
using
System.Collections.Generic;
public
class
Twitter {
public
Dictionary<
int
, HashSet<
int
> > friendsOfUser;
public
int
curr;
public
List<Tuple<
int
,
int
,
int
> > timeline;
public
Twitter()
{
friendsOfUser
=
new
Dictionary<
int
, HashSet<
int
> >();
curr = 0;
timeline =
new
List<Tuple<
int
,
int
,
int
> >();
}
public
void
postTweet(
int
userId,
int
tweetId)
{
timeline.Add(
new
Tuple<
int
,
int
,
int
>(
-curr, tweetId, userId));
curr++;
}
public
List<
int
> getNewsFeed(
int
userId)
{
List<
int
> ans =
new
List<
int
>();
List<Tuple<
int
,
int
,
int
> > userTimeline
=
new
List<Tuple<
int
,
int
,
int
> >(timeline);
userTimeline.Sort();
int
n = 0;
foreach
(
var
topTweet
in
userTimeline)
{
if
(n >= 10)
break
;
if
(topTweet.Item3 == userId
|| friendsOfUser.ContainsKey(userId)
&& friendsOfUser[userId].Contains(
topTweet.Item3)) {
ans.Add(topTweet.Item2);
n++;
}
}
return
ans;
}
public
void
follow(
int
followerId,
int
followeeId)
{
if
(!friendsOfUser.ContainsKey(followerId))
friendsOfUser[followerId] =
new
HashSet<
int
>();
friendsOfUser[followerId].Add(followeeId);
}
public
void
unfollow(
int
followerId,
int
followeeId)
{
if
(friendsOfUser.ContainsKey(followerId))
friendsOfUser[followerId].Remove(followeeId);
}
}
public
class
Program {
static
void
Main()
{
Twitter obj =
new
Twitter();
int
userId = 1;
int
tweetId = 5;
obj.postTweet(userId, tweetId);
List<
int
> vec = obj.getNewsFeed(userId);
foreach
(
int
a
in
vec) Console.Write(a +
" "
);
Console.WriteLine();
int
follower = 1;
int
followee = 2;
obj.follow(follower, followee);
userId = 2;
tweetId = 6;
obj.postTweet(userId, tweetId);
userId = 1;
vec = obj.getNewsFeed(userId);
foreach
(
int
a
in
vec) { Console.Write(a +
" "
); }
Console.WriteLine();
follower = 1;
followee = 2;
obj.unfollow(follower, followee);
Console.Write(vec[1]);
Console.WriteLine();
}
}