Similar Problems
Similar Problems not available
All The Pairs With The Maximum Number Of Common Followers - Leetcode Solution
Companies:
LeetCode: All The Pairs With The Maximum Number Of Common Followers Leetcode Solution
Difficulty: Medium
Topics: database
Problem Statement:
You are given an array of Instagram accounts, where each account is represented as a string. Each account has a list of followers, which is also represented as a string. A pair of accounts (u, v) has the maximum number of common followers if there exists a follower who follows both u and v.
Write a function that finds all pairs of accounts that have the maximum number of common followers. The result should be returned as a list of pairs (u, v).
Input: accounts = ["u1,f1,f2", "u2,f2,f3", "u3,f1,f3", "u4,f3,f4"]
Output: [('u2', 'u3'), ('u3', 'u2')]
Explanation: The maximum number of common followers between any pair of account is 2. The pairs that have 2 common followers are (u2, u3) and (u3, u2).
Solution:
The approach to solve this problem is to first create a dictionary where keys are the followers and values are the accounts that follow them. Then, we will iterate over each pair of accounts and calculate the number of common followers they have. We will keep track of max number of common followers so far and store all the pairs that have that many followers.
-
First, we create the dictionary of followers and accounts:
follower_to_accounts = {} for account in accounts: account_split = account.split(',') account_name = account_split[0] followers = account_split[1:] for f in followers: if f not in follower_to_accounts: follower_to_accounts[f] = set() follower_to_accounts[f].add(account_name)
-
Then, we iterate over each pair of accounts and calculate the number of common followers:
max_common_followers = 0 max_common_pairs = [] for i in range(len(accounts)): for j in range(i+1, len(accounts)): common_followers = len(set(accounts[i].split(',')[1:]) & set(accounts[j].split(',')[1:])) if common_followers > max_common_followers: max_common_followers = common_followers max_common_pairs = [(accounts[i].split(',')[0], accounts[j].split(',')[0])] elif common_followers == max_common_followers: max_common_pairs.append((accounts[i].split(',')[0], accounts[j].split(',')[0]))
-
Finally, we return the list of pairs that have the maximum number of common followers:
return max_common_pairs
Time Complexity:
Creating the dictionary of followers takes O(n*k) time, where n is the number of accounts and k is the average number of followers per account.
Iterating over each pair of accounts takes O(n^2) time.
Therefore, the overall time complexity of the solution is O(n^2 + n*k).
Space Complexity:
We are using a dictionary to store the followers and accounts, which takes O(n*k) space.
We are also storing the maximum number of followers and the pairs that have that many followers, which takes O(n^2) space.
Therefore, the overall space complexity of the solution is O(n*k + n^2).
All The Pairs With The Maximum Number Of Common Followers Solution Code
1