Social feed~30 min2s · 256 MBtarget O(F log F) per feed time, O(n) spaceclassic twin: lc-355k-way-merge · heaps
A social app shows each member a timeline. The timeline holds the ten newest posts from the people the member follows, and from the member too. A member can follow and unfollow others at any time.
Posts are ordered by when they were made, and the newest comes first. The app serves millions of timelines a day, so a timeline must not go through every post ever made. Your team lead asks you to build the timeline.
Write run_timeline(commands, args).
commands[i] is "post", "follow", "unfollow" or "feed". A command happens after all the commands before it."post": args[i] is [user, post]. Member user makes a post with the id post."follow" and "unfollow": args[i] is [user, other]. The member starts or stops following other. Following twice, unfollowing someone you do not follow, and following or unfollowing yourself change nothing."feed": args[i] is [user]. List the ids of the ten newest posts made so far by user and by everyone user follows now. Newest first, and fewer than ten if there are fewer."feed", in order, its post ids followed by -1. The other commands add nothing.No member follows more than 50 others at one time.
Input: commands = ["post", "post", "follow", "feed", "post", "feed", "unfollow", "feed"], args = [[1, 101], [2, 201], [1, 2], [1], [2, 202], [1], [1, 2], [1]]
Output: [201, 101, -1, 202, 201, 101, -1, 101, -1]Each author keeps a list of posts in time order. A feed merges these lists, taking the newest post first from among the lists.
| Step | Command | State | Output added |
|---|---|---|---|
| [2] | post 101 by member 1 | member 1: 101 | |
| [2] | post 201 by member 2 | member 2: 201 | |
| [2] | member 1 follows 2 | 1 follows {2} | |
| [3], [4] | feed for 1 | merge lists of 1 and 2: 201, then 101 | 201, 101, -1 |
| [2] | post 202 by member 2 | member 2: 201, 202 | |
| [3], [4] | feed for 1 | merge: 202, 201, 101 | 202, 201, 101, -1 |
| [2] | member 1 unfollows 2 | 1 follows {} | |
| [3], [4] | feed for 1 | only the own list: 101 | 101, -1 |
Input: commands = ["feed"], args = [[7]]
Output: [-1]Member 7 has no posts and follows nobody, so the feed is empty and gives only the end mark.
Input: commands = ["post", "post", "post", "post", "post", "post", "post", "post", "post", "post", "post", "post", "feed"], args = [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [1, 11], [1, 12], [1]]
Output: [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, -1]Member 1 made twelve posts. The feed shows only the newest ten.
1 ≤ length of commands ≤ 100000, and commands and args have the same length"post", "follow" or "unfollow" row has two numbers, and a "feed" row has one1 to 1000000There can be 100,000 commands, most of them posts and feeds. Going through every post made so far for each feed can cost about 50,000 × 50,000 steps, which is billions.
A member follows at most 50 others, and a feed needs only ten posts. Each author's posts are already in time order, so the newest ten can be found by merging at most 51 sorted lists, and stopping after ten.