Leetcode Problem 1951. All the Pairs With the Maximum Number of Common Followers

1951. All the Pairs With the Maximum Number of Common Followers

Leetcode Solutions

Self-Join and Aggregation with Subquery

  • Perform a self-join on the Relations table where r1.follower_id equals r2.follower_id and r1.user_id is less than r2.user_id.
  • Group the results by r1.user_id and r2.user_id.
  • Count the number of common followers for each pair and label this count as common_followers.
  • Use a subquery to find the maximum value of common_followers across all pairs.
  • Select the pairs where their count of common followers equals the maximum value found in the subquery.
  • Return the result as a new table with columns user1_id and user2_id.

erDiagram
    Relations {
        int user_id
        int follower_id
        user_id follower_id PK
    }

Using Common Table Expressions (CTEs) and Window Functions

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...