Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

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

ArrayHash TableString

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.

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;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.