Skip to content

Commit fff3cec

Browse files
authored
Merge pull request #5721 from neetcode-gh/ohtani
fix solution articles
2 parents c5ca0e8 + 8a58f33 commit fff3cec

File tree

2 files changed

+561
-527
lines changed

2 files changed

+561
-527
lines changed

articles/design-twitter-feed.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,11 @@ Instead of combining **all** tweets and sorting them (which is slow), we only ne
491491

492492
We use a **min-heap** (priority queue) because:
493493

494-
- We push only the _latest tweet_ of each followee.
495-
- Each tweet has a timestamp (`count`), where smaller means more recent.
496-
- We repeatedly extract the most recent tweet and then push the _next_ tweet from that same user.
497-
- This is similar to merging K sorted lists efficiently.
494+
- We start by pushing the latest tweet from each followee.
495+
- Smaller `count` means the tweet is more recent.
496+
- When we pop a tweet, we push the next older tweet from that same user.
497+
- We repeat this until we collect `10` tweets.
498+
- One user can still contribute many tweets if they are the most recent.
498499

499500
This ensures:
500501

@@ -518,6 +519,7 @@ This ensures:
518519
- For each followee:
519520
- Push their most recent tweet into a min-heap:
520521
`[time, tweetId, followeeId, nextIndex]`
522+
- This is only the starting state of the merge, not the final candidate set.
521523
- While heap is not empty and result has < `10` tweets:
522524
- Pop the most recent tweet
523525
- Add tweetId to the result
@@ -1171,8 +1173,9 @@ The trick:
11711173

11721174
- When a user posts a tweet, append it with a decreasing timestamp (`count`) and keep only the last `10` tweets.
11731175
- When getting the news feed:
1174-
- If the user follows many people (>= `10`), we first gather only the recent tweets that _could_ appear in the final `10`, using a max-heap limited to size `10`.
1175-
- Otherwise, push the most recent tweet of each followee directly into a min-heap and expand like a K-sorted-list merge.
1176+
- If the user follows many people (>= `10`), keep only the `10` followees with the newest latest tweet by using a max-heap of size `10`.
1177+
- This is safe: if a followee's newest tweet is already too old, none of their older tweets can make the final `10`.
1178+
- Otherwise, push the latest tweet from each followee into a min-heap and keep expanding from the same user after each pop.
11761179
- In both cases, we never process more than **`10` tweets per followee**, and never extract more than **`10` results**.
11771180

11781181
This makes the method very fast even when users post a lot of tweets.
@@ -1188,8 +1191,9 @@ This makes the method very fast even when users post a lot of tweets.
11881191
Steps:
11891192
- Ensure user follows themselves.
11901193
- If followees >= `10`:
1191-
- Build a **max-heap** that stores only the top `10` most recent tweets across followees.
1192-
- Convert it to a **min-heap** for final processing.
1194+
- Build a **max-heap** that keeps only the `10` newest latest tweets from followees.
1195+
- Move them into a **min-heap** for final processing.
1196+
- After popping a tweet, push the next older tweet from that same followee.
11931197
- Else:
11941198
- Push the newest tweet from each followee into a min-heap.
11951199
- Repeatedly pop from the heap:

0 commit comments

Comments
 (0)