People Whose List of Favorite Companies Is Not a Subset of Another List Problem
Description
LeetCode Problem 1452.
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 <= 1001 <= favoriteCompanies[i].length <= 5001 <= favoriteCompanies[i][j].length <= 20- All strings in
favoriteCompanies[i]are distinct. - All lists of favorite companies are distinct, that is, If we sort alphabetically each list then
favoriteCompanies[i] != favoriteCompanies[j]. - All strings consist of lowercase English letters only.
Solution
Approach: Hash Sets and Pairwise Subset Checks
There are at most 100 people, so we can afford to compare every pair of lists. What we must avoid is comparing strings over and over, so first give each company a number and turn every list into a set of numbers.
For person i, look at every other person j. List i is a subset of list j when every company of i is in j's set. Since all lists are distinct, a subset is always a strictly smaller set, so we can skip any j whose list is not longer than i's; this also stops a list from being compared with an equal one.
If no j contains list i, add i to the answer. Going through i in increasing order gives the indices already sorted, as required.
Complexity: O(n2 · m) time, where n is the number of people and m the longest list, and O(n · m) extra space for the sets.
class Solution:
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
# Give each company a number so the sets hold small integers instead of strings.
ids = {}
sets = [{ids.setdefault(c, len(ids)) for c in companies} for companies in favoriteCompanies]
ans = []
for i, a in enumerate(sets):
# a can only be inside a longer list (all lists are distinct, so never an equal one).
if not any(j != i and len(b) > len(a) and a <= b for j, b in enumerate(sets)):
ans.append(i)
return ans