[LeetCode] People Whose List of Favorite Companies Is Not a Subset of Another List

1452. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
bool subsetOf(unordered_set<string>& A, unordered_set<string>& B) {
for(auto& a : A) {
if(!B.count(a)) return false;
}
return true;
}
public:
vector<int> peopleIndexes(vector<vector<string>>& A) {
vector<unordered_set<string>> us;
for(auto& a : A)
us.push_back(unordered_set<string>(begin(a),end(a)));
vector<int> res;
for(int i = 0; i < A.size(); i++) {
bool subset = false;
for(int j = 0; j < A.size() and !subset; j++) {
if(i == j) continue;
subset |= subsetOf(us[i], us[j]);
}
if(!subset) res.push_back(i);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/29/PS/LeetCode/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.