{x}
blog image

People Whose List of Favorite Companies Is Not a Subset of Another List

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
  • 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 Explanation

This problem asks to find the indices of people whose favorite company lists are not subsets of any other person's list. The most efficient approach uses a hash table (or map in other languages) to manage company names and sets to represent each person's favorite companies.

Algorithm:

  1. Create a mapping: We first create a mapping (dictionary in Python, HashMap in Java, etc.) to assign a unique integer ID to each distinct company name. This allows us to represent the sets of favorite companies efficiently using integers.

  2. Convert to sets: We convert each person's list of favorite companies into a set of integer IDs (using the mapping created in step 1). Sets are used because they provide efficient membership testing, which is crucial for the subset check.

  3. Subset check: For each person, we iterate through all other people. If a person's favorite company set is a subset of another person's set, then that person's index is not added to the result.

  4. Collect and return: Finally, we collect the indices of people whose sets were not subsets of any others and return them in increasing order.

Time Complexity Analysis:

  • Step 1: Creating the company-to-ID mapping takes O(C) time, where C is the total number of unique company names across all lists.

  • Step 2: Converting each person's list to a set takes O(P * M) time in the worst case, where P is the number of people and M is the maximum number of companies in a single person's list.

  • Step 3: The subset check involves nested loops, iterating through all pairs of people. For each pair, checking if one set is a subset of another takes O(M) time in the worst case. Thus, this step takes O(P² * M) time.

  • Step 4: Collecting and returning the result takes O(P) time.

Therefore, the overall time complexity is dominated by the subset check, resulting in O(P² * M), where P is the number of people and M is the maximum number of companies a person favors.

Space Complexity Analysis:

  • Company mapping: The space required for the company-to-ID mapping is O(C), where C is the total number of unique companies.

  • Sets: The space for storing the sets of favorite companies is O(P * M), where P is the number of people and M is the maximum number of companies in a person's list.

Therefore, the overall space complexity is O(P * M + C). Since C is bounded by P * M, we can simplify this to O(P * M).

Code Examples (with minor improvements for clarity):

The provided code examples are already fairly efficient, but here's a slightly modified Python version with more concise subset checking:

from typing import List
 
class Solution:
    def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
        n = len(favoriteCompanies)
        company_map = {}
        next_id = 0
        
        # Build company map
        for companies in favoriteCompanies:
            for company in companies:
                if company not in company_map:
                    company_map[company] = next_id
                    next_id += 1
        
        # Convert to sets of IDs
        company_sets = []
        for companies in favoriteCompanies:
            company_sets.append(set(company_map[c] for c in companies))
 
        result = []
        for i in range(n):
            is_subset = False
            for j in range(n):
                if i != j and company_sets[i].issubset(company_sets[j]):
                    is_subset = True
                    break
            if not is_subset:
                result.append(i)
        return result
 

The other language examples (Java, C++, Go, TypeScript) follow similar logic with appropriate data structures for each language. They all achieve the same time and space complexity as described above.