728x90
반응형
728x90
[NeetCode-LeetCode] Design Twitter - Medium
접근
- 구현
풀이
트위터의 기능을 디자인하는 문제로 포스팅, 피드 보기, 팔로우, 언팔로우를 구현해야 하는 문제이다. 기존 sns와 동일하게 유저는 포스트를 작성할 수 있고, 자기 자신의 포스트와 팔로우하는 유저의 포스트를 볼 수 있으며 다른 유저를 팔로우, 언팔로우할 수 있다.
주의할 부분은 포스트를 얻는 부분으로 포스트는 최신 10개의 포스트를 가져와야 한다. 최신 10개의 포스트라는 것은 해당 유저가 팔로우하는 유저에 따라 변경되는 부분으로 특정 유저를 팔로우하기 시작했다면 다시 최신 10개의 포스트를 구성해야 하고 언팔로우하였다면 언팔로우한 유저의 포스트를 뺀 최신 10개를 다시 구성해야 한다. 즉 포스트 요청 시 동적으로 구성되어야 한다는 것이다.
class Twitter {
private int timestamp;
private Map<Integer, Set<Integer>> followMap;
private Map<Integer, List<int[]>> tweetMap;
public Twitter() {
timestamp = 0;
followMap = new HashMap<>();
tweetMap = new HashMap<>();
}
포스트는 최신순으로 구성되어야 하기 때문에 순서를 관리하기 위해 timestamp를 하나 두었다. 그리고 유저 간 팔로우를 관리하기 위한 맵과 포스트의 정보를 담을 맵을 사용 했다.
public void postTweet(int userId, int tweetId) {
if (!tweetMap.containsKey(userId)) {
tweetMap.put(userId, new ArrayList<>());
}
tweetMap.get(userId).add(new int[] { timestamp++, tweetId });
}
포스트를 작성하는 메서드이다. 포스트를 관리하는 트윗맵에 timestamp와 해당 트윗의 아이디를 저장한다.
public List<Integer> getNewsFeed(int userId) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]);
if (tweetMap.containsKey(userId)) {
pq.addAll(tweetMap.get(userId));
}
Set<Integer> followees = followMap.getOrDefault(userId, new HashSet<>());
for (int followee : followees) {
if (tweetMap.containsKey(followee)) {
pq.addAll(tweetMap.get(followee));
}
}
List<Integer> feed = new ArrayList<>();
while (!pq.isEmpty() && feed.size() < 10) {
feed.add(pq.poll()[1]);
}
return feed;
}
최신 10개의 포스트를 얻는 메서드이다. 자기 자신의 트윗과 팔로우하는 유저들의 트윗을 모두 우선순위 큐에 넣어준 뒤 timestamp 값이 높은 최신순으로 트윗을 10개 뽑아서 넣어준 뒤 반환한다.
public void follow(int followerId, int followeeId) {
followMap.putIfAbsent(followerId, new HashSet<>());
followMap.get(followerId).add(followeeId);
}
public void unfollow(int followerId, int followeeId) {
if (followMap.containsKey(followerId)) {
followMap.get(followerId).remove(followeeId);
}
}
}
팔로우와 언팔로우 메서드로 팔로우 맵의 연결을 이어주고 끊는 부분이다.
전체 코드
class Twitter {
private int timestamp;
private Map<Integer, Set<Integer>> followMap;
private Map<Integer, List<int[]>> tweetMap;
public Twitter() {
timestamp = 0;
followMap = new HashMap<>();
tweetMap = new HashMap<>();
}
public void postTweet(int userId, int tweetId) {
if (!tweetMap.containsKey(userId)) {
tweetMap.put(userId, new ArrayList<>());
}
tweetMap.get(userId).add(new int[] { timestamp++, tweetId });
}
public List<Integer> getNewsFeed(int userId) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]);
if (tweetMap.containsKey(userId)) {
pq.addAll(tweetMap.get(userId));
}
Set<Integer> followees = followMap.getOrDefault(userId, new HashSet<>());
for (int followee : followees) {
if (tweetMap.containsKey(followee)) {
pq.addAll(tweetMap.get(followee));
}
}
List<Integer> feed = new ArrayList<>();
while (!pq.isEmpty() && feed.size() < 10) {
feed.add(pq.poll()[1]);
}
return feed;
}
public void follow(int followerId, int followeeId) {
followMap.putIfAbsent(followerId, new HashSet<>());
followMap.get(followerId).add(followeeId);
}
public void unfollow(int followerId, int followeeId) {
if (followMap.containsKey(followerId)) {
followMap.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);
*/
728x90
'알고리즘 > 코딩테스트-NeetCode' 카테고리의 다른 글
[NeetCode-LeetCode] Max Area of Island JAVA (0) | 2025.02.26 |
---|---|
[NeetCode-LeetCode] Koko Eating Bananas Java (0) | 2025.02.25 |
[NeetCode-LeetCode] Combination Sum II JAVA (0) | 2025.02.24 |
[NeetCode-LeetCode] Last Stone Weight JAVA (1) | 2025.02.12 |
[NeetCode-LeetCode] Search a 2D Matrix JAVA (0) | 2025.02.11 |