Skip to content

Commit 0dcc6b1

Browse files
committed
feat: add solution for design twitter in rust
1 parent 08e9513 commit 0dcc6b1

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/medium/design_twitter.rs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#![allow(dead_code)]
2+
use std::collections::BTreeMap;
3+
use std::collections::BTreeSet;
4+
use std::collections::VecDeque;
5+
6+
#[derive(Default)]
7+
struct Twitter {
8+
users: BTreeMap<i32, BTreeSet<i32>>,
9+
tweets: VecDeque<(i32, i32)>,
10+
}
11+
12+
impl Twitter {
13+
fn new() -> Self {
14+
Self::default()
15+
}
16+
17+
fn post_tweet(&mut self, user_id: i32, tweet_id: i32) {
18+
self.check_and_create_user_if_not_exist(user_id);
19+
self.tweets.push_front((user_id, tweet_id));
20+
}
21+
22+
fn get_news_feed(&mut self, user_id: i32) -> Vec<i32> {
23+
self.check_and_create_user_if_not_exist(user_id);
24+
let mut res = Vec::new();
25+
let following = self.users.get(&user_id).unwrap();
26+
for (usr_id, tweet_id) in &self.tweets {
27+
if *usr_id == user_id || following.contains(&usr_id) {
28+
res.push(*tweet_id);
29+
if res.len() == 10 {
30+
break;
31+
}
32+
}
33+
}
34+
res
35+
}
36+
37+
fn follow(&mut self, follower_id: i32, followee_id: i32) {
38+
self.check_and_create_user_if_not_exist(follower_id);
39+
self.users
40+
.get_mut(&follower_id)
41+
.unwrap()
42+
.insert(followee_id);
43+
}
44+
45+
fn unfollow(&mut self, follower_id: i32, followee_id: i32) {
46+
self.check_and_create_user_if_not_exist(follower_id);
47+
self.users
48+
.get_mut(&follower_id)
49+
.unwrap()
50+
.remove(&followee_id);
51+
}
52+
53+
fn check_and_create_user_if_not_exist(&mut self, follower_id: i32) {
54+
if let None = self.users.get(&follower_id) {
55+
self.users.insert(follower_id, BTreeSet::new());
56+
}
57+
}
58+
}
59+
60+
#[cfg(test)]
61+
mod tests {
62+
use super::*;
63+
64+
#[test]
65+
fn test_twitter() {
66+
let mut twitter = Twitter::new();
67+
twitter.post_tweet(1, 5);
68+
assert_eq!(twitter.get_news_feed(1), vec![5]);
69+
twitter.follow(1, 2);
70+
twitter.post_tweet(2, 6);
71+
assert_eq!(twitter.get_news_feed(1), vec![6, 5]);
72+
twitter.unfollow(1, 2);
73+
assert_eq!(twitter.get_news_feed(1), vec![5]);
74+
}
75+
}

src/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
- [x] [271. Encode and decode strings](../src/medium/encode_and_decode_strings.rs) -> [Problem Description](../src/medium/readme.md#271-encode-and-decode-strings)
8484
- [x] [287. Find the duplicate number](../src/medium/find_the_duplicate_number.rs) -> [Problem Description](../src/medium/readme.md#287-find-the-duplicate-number)
8585
- [x] [347. Top k frequent elements](../src/medium/top_k_frequent_elements.rs) -> [Problem Description](../src/medium/readme.md#347-top-k-frequent-elements)
86-
- [ ] [355. Design twitter](../src/medium/design_twitter.rs) -> [Problem Description](../src/medium/readme.md#355-design-twitter)
86+
- [x] [355. Design twitter](../src/medium/design_twitter.rs) -> [Problem Description](../src/medium/readme.md#355-design-twitter)
8787
- [x] [424. Longest repeating character replacement](../src/medium/longest_repeating_character_replacement.rs) -> [Problem Description](../src/medium/readme.md#424-longest-repeating-character-replacement)
8888
- [x] [567. Permutation in string](../src/medium/permutation_in_string.rs) -> [Problem Description](../src/medium/readme.md#567-permutation-in-string)
8989
- [x] [621. Task scheduler](../src/medium/task_scheduler.rs) -> [Problem Description](../src/medium/readme.md#621-task-scheduler)

theory/categories/6.heap_priority_queue/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ A priority queue can be implemented using a heap.
6868
- [x] [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | Medium | [Solution](../../../src/medium/k_closest_points_to_origin.rs) | [Problem Description](../../../src/medium/readme.md#973-k-closest-points-to-origin)
6969
- [x] [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | Medium | [Solution](../../../src/medium/kth_largest_element_in_an_array.rs) | [Problem Description](../../../src/medium/readme.md#215-kth-largest-element-in-an-array)
7070
- [x] [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | Medium | [Solution](../../../src/medium/task_scheduler.rs) | [Problem Description](../../../src/medium/readme.md#621-task-scheduler)
71-
- [ ] [Design Twitter](https://leetcode.com/problems/design-twitter/) | Medium | [Solution](../../../src/medium/design_twitter.rs) | [Problem Description](../../../src/medium/readme.md#355-design-twitter)
71+
- [x] [Design Twitter](https://leetcode.com/problems/design-twitter/) | Medium | [Solution](../../../src/medium/design_twitter.rs) | [Problem Description](../../../src/medium/readme.md#355-design-twitter)
7272
- [ ] [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | Hard | [Solution](../../../src/hard/find_median_from_data_stream.rs) | [Problem Description](../../../src/hard/readme.md#295-find-median-from-data-stream)
7373

7474
Category: `Heap / Priority Queue`
7575
Created on: 2023-11-02 21:12
76-
Last modified on: 2023-11-03 22:13
76+
Last modified on: 2023-11-04 00:06
7777
Status: In Progress
7878
Author: [David Bujosa](https://github.com/bujosa)

0 commit comments

Comments
 (0)