Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df56b15

Browse files
committedNov 4, 2023
docs: add problem description for 355. Design Twitter
1 parent 40a586f commit df56b15

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
 

‎src/medium/readme.md

+50
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,56 @@ Put the code below in main.rs and run `cargo run`
13011301
println!("result: {:?}", result);
13021302
```
13031303

1304+
# 355. Design Twitter
1305+
1306+
## Description
1307+
1308+
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
1309+
1310+
Implement the `Twitter` class:
1311+
1312+
- `Twitter()` Initializes your twitter object.
1313+
- `void postTweet(int userId, int tweetId)` Composes a new tweet with ID `tweetId` by the user userId. Each call to this function will be made with a unique tweetId.
1314+
- `List<Integer> getNewsFeed(int userId)` Retrieves 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 themself. Tweets must be ordered from most recent to least recent.
1315+
- `void follow(int followerId, int followeeId) `The user with ID `followerId` started following the user with ID `followeeId`.
1316+
- `void unfollow(int followerId, int followeeId)` The user with ID `followerId` started unfollowing the user with ID `followeeId`.
1317+
1318+
## Examples
1319+
1320+
```text
1321+
Input
1322+
["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"]
1323+
[[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]]
1324+
Output
1325+
[null, null, [5], null, null, [6, 5], null, [5]]
1326+
1327+
Explanation
1328+
Twitter twitter = new Twitter();
1329+
twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5).
1330+
twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5]. return [5]
1331+
twitter.follow(1, 2); // User 1 follows user 2.
1332+
twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6).
1333+
twitter.getNewsFeed(1); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
1334+
twitter.unfollow(1, 2); // User 1 unfollows user 2.
1335+
twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5], since user 1 is no longer following user 2.
1336+
```
1337+
1338+
## How to Run in main.rs
1339+
1340+
Put the code below in main.rs and run `cargo run`
1341+
1342+
```rust
1343+
use leetcode::medium::design_twitter::Twitter;
1344+
let mut twitter = Twitter::new();
1345+
twitter.post_tweet(1, 5);
1346+
println!("{:?}", twitter.get_news_feed(1));
1347+
twitter.follow(1, 2);
1348+
twitter.post_tweet(2, 6);
1349+
println!("{:?}", twitter.get_news_feed(1));
1350+
twitter.unfollow(1, 2);
1351+
println!("{:?}", twitter.get_news_feed(1));
1352+
```
1353+
13041354
# 424. Longest Repeating Character Replacement
13051355

13061356
## Description

‎src/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +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)
8687
- [x] [424. Longest repeating character replacement](../src/medium/longest_repeating_character_replacement.rs) -> [Problem Description](../src/medium/readme.md#424-longest-repeating-character-replacement)
8788
- [x] [567. Permutation in string](../src/medium/permutation_in_string.rs) -> [Problem Description](../src/medium/readme.md#567-permutation-in-string)
8889
- [x] [621. Task scheduler](../src/medium/task_scheduler.rs) -> [Problem Description](../src/medium/readme.md#621-task-scheduler)

0 commit comments

Comments
 (0)
Please sign in to comment.