-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign Twitter.java
More file actions
109 lines (92 loc) · 3.36 KB
/
Copy pathDesign Twitter.java
File metadata and controls
109 lines (92 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Object
{
ArrayList<Integer> list;
int index;
public Object(ArrayList<Integer> list, int index){
this.list=list;
this.index=index;
}
}
class Twitter {
/** Initialize your data structure here. */
HashMap<Integer, HashSet<Integer>> followlist;
HashMap<Integer, ArrayList<Integer>> tweetlist;
HashMap<Integer, Integer> orderlist;
int order;
public Twitter() {
followlist = new HashMap<Integer, HashSet<Integer>>();
tweetlist = new HashMap<Integer, ArrayList<Integer>>();
orderlist = new HashMap<Integer, Integer>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
ArrayList<Integer> list = tweetlist.get(userId);
if(list == null)
{
list = new ArrayList<Integer>();
tweetlist.put(userId, list);
}
list.add(tweetId);
orderlist.put(tweetId, order++);
follow(userId, userId);
}
/** 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. */
public List<Integer> getNewsFeed(int userId) {
HashSet<Integer> set = followlist.get(userId);
if(set==null)
return new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
for(int id : set)
{
if(tweetlist.get(id)!=null && tweetlist.get(id).size()>0)
lists.add(tweetlist.get(id));
}
ArrayList<Integer> result = new ArrayList<Integer>();
PriorityQueue<Object> queue = new PriorityQueue<Object>(new Comparator<Object>(){
public int compare(Object a, Object b)
{
return orderlist.get(b.list.get(b.index)) - orderlist.get(a.list.get(a.index));
}
});
for(ArrayList<Integer> list : lists)
{
queue.add(new Object(list, list.size()-1));
}
while(!queue.isEmpty() && result.size()<10)
{
Object x = queue.poll();
result.add(x.list.get(x.index));
x.index--;
if(x.index >= 0)
queue.add(x);
}
return result;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
HashSet<Integer> set = followlist.get(followerId);
if(set == null)
{
set = new HashSet<Integer>();
followlist.put(followerId, set);
}
set.add(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if(followerId == followeeId)
return;
HashSet<Integer> set = followlist.get(followerId);
if(set == null)
return;
set.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);
*/