Skip to content

Commit fb4f089

Browse files
committed
feat: add solutions to lc problem: No.1452
No.1452.People Whose List of Favorite Companies Is Not a Subset of Another List
1 parent 060bcd5 commit fb4f089

File tree

9 files changed

+473
-6
lines changed

9 files changed

+473
-6
lines changed

solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README.md

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,184 @@ favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetco
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:哈希表**
56+
57+
将每个 `company` 字符串列表都转换为一个整数类型的集合。然后遍历每个集合,判断其是否是其他集合的子集,如果不是,则将其下标加入结果集。
58+
59+
时间复杂度 $O(n^2 \times m)$,其中 $n$ 为 `favoriteCompanies` 的长度,$m$ 为 `favoriteCompanies[i]` 的最大长度。
60+
5561
<!-- tabs:start -->
5662

5763
### **Python3**
5864

5965
<!-- 这里可写当前语言的特殊实现逻辑 -->
6066

6167
```python
62-
68+
class Solution:
69+
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
70+
d = {}
71+
idx = 0
72+
t = []
73+
for v in favoriteCompanies:
74+
for c in v:
75+
if c not in d:
76+
d[c] = idx
77+
idx += 1
78+
t.append({d[c] for c in v})
79+
ans = []
80+
for i, nums1 in enumerate(t):
81+
ok = True
82+
for j, nums2 in enumerate(t):
83+
if i == j:
84+
continue
85+
if not (nums1 - nums2):
86+
ok = False
87+
break
88+
if ok:
89+
ans.append(i)
90+
return ans
6391
```
6492

6593
### **Java**
6694

6795
<!-- 这里可写当前语言的特殊实现逻辑 -->
6896

6997
```java
98+
class Solution {
99+
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
100+
Map<String, Integer> d = new HashMap<>();
101+
int idx = 0;
102+
int n = favoriteCompanies.size();
103+
Set<Integer>[] t = new Set[n];
104+
for (int i = 0; i < n; ++i) {
105+
var v = favoriteCompanies.get(i);
106+
for (var c : v) {
107+
if (!d.containsKey(c)) {
108+
d.put(c, idx++);
109+
}
110+
}
111+
Set<Integer> s = new HashSet<>();
112+
for (var c : v) {
113+
s.add(d.get(c));
114+
}
115+
t[i] = s;
116+
}
117+
List<Integer> ans = new ArrayList<>();
118+
for (int i = 0; i < n; ++i) {
119+
boolean ok = true;
120+
for (int j = 0; j < n; ++j) {
121+
if (i != j) {
122+
if (t[j].containsAll(t[i])) {
123+
ok = false;
124+
break;
125+
}
126+
}
127+
}
128+
if (ok) {
129+
ans.add(i);
130+
}
131+
}
132+
return ans;
133+
}
134+
}
135+
```
136+
137+
### **C++**
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
143+
unordered_map<string, int> d;
144+
int idx = 0, n = favoriteCompanies.size();
145+
vector<unordered_set<int>> t(n);
146+
for (int i = 0; i < n; ++i) {
147+
auto v = favoriteCompanies[i];
148+
for (auto& c : v) {
149+
if (!d.count(c)) {
150+
d[c] = idx++;
151+
}
152+
}
153+
unordered_set<int> s;
154+
for (auto& c : v) {
155+
s.insert(d[c]);
156+
}
157+
t[i] = s;
158+
}
159+
vector<int> ans;
160+
for (int i = 0; i < n; ++i) {
161+
bool ok = true;
162+
for (int j = 0; j < n; ++j) {
163+
if (i == j) continue;
164+
if (check(t[i], t[j])) {
165+
ok = false;
166+
break;
167+
}
168+
}
169+
if (ok) {
170+
ans.push_back(i);
171+
}
172+
}
173+
return ans;
174+
}
175+
176+
bool check(unordered_set<int>& nums1, unordered_set<int>& nums2) {
177+
for (int v : nums1) {
178+
if (!nums2.count(v)) {
179+
return false;
180+
}
181+
}
182+
return true;
183+
}
184+
};
185+
```
70186

187+
### **Go**
188+
189+
```go
190+
func peopleIndexes(favoriteCompanies [][]string) []int {
191+
d := map[string]int{}
192+
idx, n := 0, len(favoriteCompanies)
193+
t := make([]map[int]bool, n)
194+
for i, v := range favoriteCompanies {
195+
for _, c := range v {
196+
if _, ok := d[c]; !ok {
197+
d[c] = idx
198+
idx++
199+
}
200+
}
201+
s := map[int]bool{}
202+
for _, c := range v {
203+
s[d[c]] = true
204+
}
205+
t[i] = s
206+
}
207+
ans := []int{}
208+
check := func(nums1, nums2 map[int]bool) bool {
209+
for v, _ := range nums1 {
210+
if _, ok := nums2[v]; !ok {
211+
return false
212+
}
213+
}
214+
return true
215+
}
216+
for i := 0; i < n; i++ {
217+
ok := true
218+
for j := 0; j < n; j++ {
219+
if i == j {
220+
continue
221+
}
222+
if check(t[i], t[j]) {
223+
ok = false
224+
break
225+
}
226+
}
227+
if ok {
228+
ans = append(ans, i)
229+
}
230+
}
231+
return ans
232+
}
71233
```
72234

73235
### **...**

solution/1400-1499/1452.People Whose List of Favorite Companies Is Not a Subset of Another List/README_EN.md

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,169 @@ Other lists of favorite companies are not a subset of another list, therefore, t
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
59+
d = {}
60+
idx = 0
61+
t = []
62+
for v in favoriteCompanies:
63+
for c in v:
64+
if c not in d:
65+
d[c] = idx
66+
idx += 1
67+
t.append({d[c] for c in v})
68+
ans = []
69+
for i, nums1 in enumerate(t):
70+
ok = True
71+
for j, nums2 in enumerate(t):
72+
if i == j:
73+
continue
74+
if not (nums1 - nums2):
75+
ok = False
76+
break
77+
if ok:
78+
ans.append(i)
79+
return ans
5880
```
5981

6082
### **Java**
6183

6284
```java
85+
class Solution {
86+
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
87+
Map<String, Integer> d = new HashMap<>();
88+
int idx = 0;
89+
int n = favoriteCompanies.size();
90+
Set<Integer>[] t = new Set[n];
91+
for (int i = 0; i < n; ++i) {
92+
var v = favoriteCompanies.get(i);
93+
for (var c : v) {
94+
if (!d.containsKey(c)) {
95+
d.put(c, idx++);
96+
}
97+
}
98+
Set<Integer> s = new HashSet<>();
99+
for (var c : v) {
100+
s.add(d.get(c));
101+
}
102+
t[i] = s;
103+
}
104+
List<Integer> ans = new ArrayList<>();
105+
for (int i = 0; i < n; ++i) {
106+
boolean ok = true;
107+
for (int j = 0; j < n; ++j) {
108+
if (i != j) {
109+
if (t[j].containsAll(t[i])) {
110+
ok = false;
111+
break;
112+
}
113+
}
114+
}
115+
if (ok) {
116+
ans.add(i);
117+
}
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
130+
unordered_map<string, int> d;
131+
int idx = 0, n = favoriteCompanies.size();
132+
vector<unordered_set<int>> t(n);
133+
for (int i = 0; i < n; ++i) {
134+
auto v = favoriteCompanies[i];
135+
for (auto& c : v) {
136+
if (!d.count(c)) {
137+
d[c] = idx++;
138+
}
139+
}
140+
unordered_set<int> s;
141+
for (auto& c : v) {
142+
s.insert(d[c]);
143+
}
144+
t[i] = s;
145+
}
146+
vector<int> ans;
147+
for (int i = 0; i < n; ++i) {
148+
bool ok = true;
149+
for (int j = 0; j < n; ++j) {
150+
if (i == j) continue;
151+
if (check(t[i], t[j])) {
152+
ok = false;
153+
break;
154+
}
155+
}
156+
if (ok) {
157+
ans.push_back(i);
158+
}
159+
}
160+
return ans;
161+
}
162+
163+
bool check(unordered_set<int>& nums1, unordered_set<int>& nums2) {
164+
for (int v : nums1) {
165+
if (!nums2.count(v)) {
166+
return false;
167+
}
168+
}
169+
return true;
170+
}
171+
};
172+
```
63173

174+
### **Go**
175+
176+
```go
177+
func peopleIndexes(favoriteCompanies [][]string) []int {
178+
d := map[string]int{}
179+
idx, n := 0, len(favoriteCompanies)
180+
t := make([]map[int]bool, n)
181+
for i, v := range favoriteCompanies {
182+
for _, c := range v {
183+
if _, ok := d[c]; !ok {
184+
d[c] = idx
185+
idx++
186+
}
187+
}
188+
s := map[int]bool{}
189+
for _, c := range v {
190+
s[d[c]] = true
191+
}
192+
t[i] = s
193+
}
194+
ans := []int{}
195+
check := func(nums1, nums2 map[int]bool) bool {
196+
for v, _ := range nums1 {
197+
if _, ok := nums2[v]; !ok {
198+
return false
199+
}
200+
}
201+
return true
202+
}
203+
for i := 0; i < n; i++ {
204+
ok := true
205+
for j := 0; j < n; j++ {
206+
if i == j {
207+
continue
208+
}
209+
if check(t[i], t[j]) {
210+
ok = false
211+
break
212+
}
213+
}
214+
if ok {
215+
ans = append(ans, i)
216+
}
217+
}
218+
return ans
219+
}
64220
```
65221

66222
### **...**

0 commit comments

Comments
 (0)