Given the array favoriteCompanies
where favoriteCompanies[i]
is the list of favorites companies for the ith
person (indexed from 0).
Return the indices of people whose list of favorite companies is not a subset of any other list of favorites companies. You must return the indices in increasing order.
Example 1:
Input: favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]] Output: [0,1,4] Explanation: Person with index=2 has favoriteCompanies[2]=["google","facebook"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] corresponding to the person with index 0. Person with index=3 has favoriteCompanies[3]=["google"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] and favoriteCompanies[1]=["google","microsoft"]. Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].
Example 2:
Input: favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]] Output: [0,1] Explanation: In this case favoriteCompanies[2]=["facebook","google"] is a subset of favoriteCompanies[0]=["leetcode","google","facebook"], therefore, the answer is [0,1].
Example 3:
Input: favoriteCompanies = [["leetcode"],["google"],["facebook"],["amazon"]] Output: [0,1,2,3]
Constraints:
1 <= favoriteCompanies.length <= 100
1 <= favoriteCompanies[i].length <= 500
1 <= favoriteCompanies[i][j].length <= 20
favoriteCompanies[i]
are distinct.favoriteCompanies[i] != favoriteCompanies[j].
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given lists of favorite companies for different people, and we want to find people whose list isn't contained within anyone else's list. The brute force way is to simply compare each person's list to every other person's list to check for subsets.
Here's how the algorithm would work step-by-step:
def find_non_subset_people(favorite_companies_lists):
result = []
number_of_people = len(favorite_companies_lists)
for i in range(number_of_people):
is_subset = False
for j in range(number_of_people):
# Skip comparing a list to itself
if i == j:
continue
# Check if the current person's list is a subset of another person's list
if set(favorite_companies_lists[i]).issubset(set(favorite_companies_lists[j])):
is_subset = True
# If it's a subset, no need to compare with other lists
break
# If the person's list is not a subset of any other list
if not is_subset:
# Add the person's index to the result
result.append(i)
return result
The goal is to find people whose favorite companies are unique, meaning their list isn't fully contained within someone else's list. We can achieve this efficiently by comparing each person's list to all other lists and marking those that are subsets.
Here's how the algorithm would work step-by-step:
def find_non_subset_people(favorite_companies):
number_of_people = len(favorite_companies)
non_subset_people = []
is_subset = [False] * number_of_people
for i in range(number_of_people):
# Compare the current person's list with everyone else's.
for j in range(number_of_people):
if i == j:
continue
# Check if favorite_companies[i] is a subset of favorite_companies[j].
if set(favorite_companies[i]).issubset(set(favorite_companies[j])):
# Mark person i as a subset
is_subset[i] = True
break
# Collect people who are not subsets of any other list.
for i in range(number_of_people):
if not is_subset[i]:
non_subset_people.append(i)
return non_subset_people
Case | How to Handle |
---|---|
Input list of people is null or empty | Return an empty list indicating no people can be identified. |
A person's favorite company list is null or empty | Treat a person with an empty favorite companies list as not being a subset of any other list, adding their ID to the result if no other person also has an empty list. |
Two or more people have identical lists of favorite companies | The problem doesn't specify distinct people, so treat them as distinct, and only keep one of the duplicated list from the result. |
One person's list is exactly the same as all other people's favorite companies. | This person will not be in the result, because their list *is* a subset of everyone else's list, unless everyone else has this same list. |
Company names contain unusual characters or are case-sensitive but should be treated case-insensitively | Normalize the company names to lowercase during processing, or use a case-insensitive comparison method. |
Maximum number of people or maximum number of companies in a person's favorite list causing memory overflow or performance issues | Optimize for space complexity using appropriate data structures (e.g. sets) and algorithm (avoiding unnecessary comparisons). |
Integer overflow when computing hashes of the company lists | Use a sufficiently large hash function, consider using modulo operator or cryptographic hash function to prevent collisions |
No one's list is a subset of another person's list. | Return the list of all people's IDs because every person satisfies the condition. |