Leetcode 1452: People Whose List of Favorite Companies Is Not a Subset of Another List

grid47
grid47
Exploring patterns and algorithms
Jun 14, 2024 6 min read

Given an array of lists where favoriteCompanies[i] represents the favorite companies of the i-th person, find the indices of people whose favorite company lists are not subsets of any other person’s list. Return these indices in increasing order.
Problem
Approach
Steps
Complexity
Input: The input is a 2D array where each element is a list of strings representing the favorite companies of a person.
Example: Input: favoriteCompanies = [["apple","google","amazon"],["apple","microsoft"],["google","apple"],["amazon"]]
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 are distinct.
• Strings consist of lowercase English letters only.
Output: Return a list of indices of people whose favorite companies are not subsets of any other list, in increasing order.
Example: Output: [0,1,3]
Constraints:
• The output indices must be in sorted order.
• If a person's list is not a subset of any other, their index must be included.
Goal: Identify all people whose favorite company lists are unique and not subsets of other people's lists.
Steps:
• Sort each person's favorite company list for efficient subset checking.
• For each person, compare their list with all other people's lists.
• If their list is not a subset of any other list, mark their index for inclusion.
• Return all such indices in increasing order.
Goal: Conditions that must be met for the input and output.
Steps:
• Each list in the input contains only distinct company names.
• Two different lists are guaranteed to be distinct even if sorted.
• Indices in the output must be sorted in ascending order.
Assumptions:
• The input lists are valid and adhere to the constraints.
• Subset checking is performed using sorted lists for efficiency.
Input: Input: favoriteCompanies = [["apple","google"],["apple","microsoft"],["google"],["amazon"]]
Explanation: Output: [0,1,3]. Person at index 2 has a list ["google"] which is a subset of ["apple","google"]. Others are unique.

Input: Input: favoriteCompanies = [["facebook","instagram"],["instagram"],["twitter"],["facebook"]]
Explanation: Output: [0,2,3]. The list at index 1 is a subset of the list at index 0.

Input: Input: favoriteCompanies = [["youtube"],["netflix"],["amazon"],["hulu"]]
Explanation: Output: [0,1,2,3]. All lists are unique and not subsets of one another.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus