Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Twitter {
- Map<Integer, Set<Integer>> follow;
- ArrayList<ArrayList<Integer>> tweet;
- public Twitter() {
- this.follow = new HashMap<Integer, Set<Integer>> ();
- this.tweet = new ArrayList<ArrayList<Integer>> ();
- }
- public void postTweet(int userId, int tweetId) {
- tweet.add(new ArrayList<Integer>(List.of(userId, tweetId)));
- }
- public List<Integer> getNewsFeed(int userId) {
- ArrayList<Integer> NewsFeed = new ArrayList<Integer> ();
- for(int i = tweet.size() - 1; i >= 0 && NewsFeed.size() < 10; i--)
- {
- int tweetUserId = tweet.get(i).get(0);
- int tweetId = tweet.get(i).get(1);
- if(tweet.get(i).get(0) == userId) {
- NewsFeed.add(tweetId);
- }
- else if(follow.containsKey(userId) && follow.get(userId).contains(tweetUserId)) {
- NewsFeed.add(tweetId);
- }
- }
- return NewsFeed;
- }
- public void follow(int followerId, int followeeId) {
- follow.putIfAbsent(followerId, new HashSet<Integer> ());
- follow.get(followerId).add(followeeId);
- }
- public void unfollow(int followerId, int followeeId) {
- if(follow.containsKey(followerId)) {
- follow.get(followerId).remove(followeeId);
- }
- }
- }
- /**
- * Your Twitter object will be instantiated and called as such:
- * Twitter obj = new Twitter();
- * obj.postTweet(userId,tweetId);
- * List<Integer> param_2 = obj.getNewsFeed(userId);
- * obj.follow(followerId,followeeId);
- * obj.unfollow(followerId,followeeId);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement