LeetCode 1452: People Whose List of Favorite Companies Is Not a Subset of Another List Solution
Master LeetCode problem 1452 (People Whose List of Favorite Companies Is Not a Subset of Another List), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1452. People Whose List of Favorite Companies Is Not a Subset of Another List
Problem Explanation
Explanation
To solve this problem, we can iterate over each person's list of favorite companies and check if it is a subset of any other person's list. We can do this by comparing each person's list with all other lists. If a person's list is not a subset of any other list, we add that person's index to the result.
- Create a set for each person's list of favorite companies.
- Iterate over each person's list and compare it with all other lists.
- If a person's list is a subset of any other list, mark that person's index.
- Return the indices of people whose list of favorite companies is not a subset of any other list.
Time Complexity: O(n^2 * m), where n is the number of people and m is the average length of a list of favorite companies. Space Complexity: O(n * m), where n is the number of people and m is the average length of a list of favorite companies.
Solution Code
import java.util.*;
class Solution {
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
List<Integer> result = new ArrayList<>();
Set<String>[] sets = new HashSet[favoriteCompanies.size()];
for (int i = 0; i < favoriteCompanies.size(); i++) {
sets[i] = new HashSet<>(favoriteCompanies.get(i));
}
for (int i = 0; i < favoriteCompanies.size(); i++) {
boolean isSubset = false;
for (int j = 0; j < favoriteCompanies.size(); j++) {
if (i != j && sets[j].containsAll(sets[i])) {
isSubset = true;
break;
}
}
if (!isSubset) {
result.add(i);
}
}
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1452 (People Whose List of Favorite Companies Is Not a Subset of Another List)?
This page provides optimized solutions for LeetCode problem 1452 (People Whose List of Favorite Companies Is Not a Subset of Another List) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1452 (People Whose List of Favorite Companies Is Not a Subset of Another List)?
The time complexity for LeetCode 1452 (People Whose List of Favorite Companies Is Not a Subset of Another List) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1452 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1452 in Java, C++, or Python.