29
29
<p ><strong >示例 1:</strong ></p >
30
30
31
31
<pre ><strong >输入:</strong >favoriteCompanies = [[" ; leetcode" ; ," ; google" ; ," ; facebook" ; ],[" ; google" ; ," ; microsoft" ; ],[" ; google" ; ," ; facebook" ; ],[" ; google" ; ],[" ; amazon" ; ]]
32
- <strong >输出:</strong >[0,1,4]
32
+ <strong >输出:</strong >[0,1,4]
33
33
<strong >解释:</strong >
34
34
favoriteCompanies[2]=[" ; google" ; ," ; facebook" ; ] 是 favoriteCompanies[0]=[" ; leetcode" ; ," ; google" ; ," ; facebook" ; ] 的子集。
35
35
favoriteCompanies[3]=[" ; google" ; ] 是 favoriteCompanies[0]=[" ; leetcode" ; ," ; google" ; ," ; facebook" ; ] 和 favoriteCompanies[1]=[" ; google" ; ," ; microsoft" ; ] 的子集。
@@ -39,7 +39,7 @@ favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetco
39
39
<p ><strong >示例 2:</strong ></p >
40
40
41
41
<pre ><strong >输入:</strong >favoriteCompanies = [[" ; leetcode" ; ," ; google" ; ," ; facebook" ; ],[" ; leetcode" ; ," ; amazon" ; ],[" ; facebook" ; ," ; google" ; ]]
42
- <strong >输出:</strong >[0,1]
42
+ <strong >输出:</strong >[0,1]
43
43
<strong >解释:</strong >favoriteCompanies[2]=[" ; facebook" ; ," ; google" ; ] 是 favoriteCompanies[0]=[" ; leetcode" ; ," ; google" ; ," ; facebook" ; ] 的子集,因此,答案为 [0,1] 。
44
44
</pre >
45
45
@@ -70,9 +70,9 @@ favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetco
70
70
71
71
### 方法一:哈希表
72
72
73
- 将每个 ` company ` 字符串列表都转换为一个整数类型的集合。然后遍历每个集合,判断其是否是其他集合的子集,如果不是,则将其下标加入结果集 。
73
+ 我们可以将每个公司映射到一个唯一的整数,然后对于每个人,我们将他们收藏的公司转换为整数集合,最后判断是否存在一个人的收藏公司是另一个人的子集 。
74
74
75
- 时间复杂度 $O(n ^2 \times m)$,其中 $n$ 为 ` favoriteCompanies ` 的长度, $m$ 为 ` favoriteCompanies[i] ` 的最大长度 。
75
+ 时间复杂度 $(n \times m \times k + n ^2 \times m)$,空间复杂度 $O(n \times m)$。 其中 $n$ 和 $m$ 分别是 ` favoriteCompanies ` 的长度和每个公司清单的平均长度,而 $k$ 是每个公司的平均长度 。
76
76
77
77
<!-- tabs:start -->
78
78
@@ -81,25 +81,19 @@ favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetco
81
81
``` python
82
82
class Solution :
83
83
def peopleIndexes (self , favoriteCompanies : List[List[str ]]) -> List[int ]:
84
- d = {}
85
84
idx = 0
86
- t = []
87
- for v in favoriteCompanies:
88
- for c in v:
89
- if c not in d:
90
- d[c] = idx
85
+ d = {}
86
+ n = len (favoriteCompanies)
87
+ nums = [set () for _ in range (n)]
88
+ for i, ss in enumerate (favoriteCompanies):
89
+ for s in ss:
90
+ if s not in d:
91
+ d[s] = idx
91
92
idx += 1
92
- t.append({d[c] for c in v} )
93
+ nums[i].add(d[s] )
93
94
ans = []
94
- for i, nums1 in enumerate (t):
95
- ok = True
96
- for j, nums2 in enumerate (t):
97
- if i == j:
98
- continue
99
- if not (nums1 - nums2):
100
- ok = False
101
- break
102
- if ok:
95
+ for i in range (n):
96
+ if not any (i != j and (nums[i] & nums[j]) == nums[i] for j in range (n)):
103
97
ans.append(i)
104
98
return ans
105
99
```
@@ -109,32 +103,26 @@ class Solution:
109
103
``` java
110
104
class Solution {
111
105
public List<Integer > peopleIndexes (List<List<String > > favoriteCompanies ) {
106
+ int n = favoriteCompanies. size();
112
107
Map<String , Integer > d = new HashMap<> ();
113
108
int idx = 0 ;
114
- int n = favoriteCompanies . size() ;
115
- Set< Integer > [] t = new Set [n] ;
109
+ Set< Integer > [] nums = new Set [n] ;
110
+ Arrays . setAll(nums, i - > new HashSet<> ()) ;
116
111
for (int i = 0 ; i < n; ++ i) {
117
- var v = favoriteCompanies. get(i);
118
- for (var c : v ) {
119
- if (! d. containsKey(c )) {
120
- d. put(c , idx++ );
112
+ var ss = favoriteCompanies. get(i);
113
+ for (var s : ss ) {
114
+ if (! d. containsKey(s )) {
115
+ d. put(s , idx++ );
121
116
}
117
+ nums[i]. add(d. get(s));
122
118
}
123
- Set<Integer > s = new HashSet<> ();
124
- for (var c : v) {
125
- s. add(d. get(c));
126
- }
127
- t[i] = s;
128
119
}
129
120
List<Integer > ans = new ArrayList<> ();
130
121
for (int i = 0 ; i < n; ++ i) {
131
122
boolean ok = true ;
132
- for (int j = 0 ; j < n; ++ j) {
133
- if (i != j) {
134
- if (t[j]. containsAll(t[i])) {
135
- ok = false ;
136
- break ;
137
- }
123
+ for (int j = 0 ; j < n && ok; ++ j) {
124
+ if (i != j && nums[j]. containsAll(nums[i])) {
125
+ ok = false ;
138
126
}
139
127
}
140
128
if (ok) {
@@ -152,95 +140,132 @@ class Solution {
152
140
class Solution {
153
141
public:
154
142
vector<int > peopleIndexes(vector<vector<string >>& favoriteCompanies) {
143
+ int n = favoriteCompanies.size();
155
144
unordered_map<string, int> d;
156
- int idx = 0, n = favoriteCompanies.size();
157
- vector<unordered_set<int >> t(n);
145
+ int idx = 0;
146
+ vector<unordered_set<int >> nums(n);
147
+
158
148
for (int i = 0; i < n; ++i) {
159
- auto v = favoriteCompanies[ i] ;
160
- for (auto& c : v) {
161
- if (!d.count(c)) {
162
- d[ c] = idx++;
149
+ for (const auto& s : favoriteCompanies[i]) {
150
+ if (!d.contains(s)) {
151
+ d[s] = idx++;
163
152
}
153
+ nums[i].insert(d[s]);
164
154
}
165
- unordered_set<int > s;
166
- for (auto& c : v) {
167
- s.insert(d[ c] );
168
- }
169
- t[ i] = s;
170
155
}
156
+
157
+ auto check = [](const unordered_set<int >& a, const unordered_set<int >& b) {
158
+ for (int x : a) {
159
+ if (!b.contains(x)) {
160
+ return false;
161
+ }
162
+ }
163
+ return true;
164
+ };
165
+
171
166
vector<int> ans;
172
167
for (int i = 0; i < n; ++i) {
173
168
bool ok = true;
174
- for (int j = 0; j < n; ++j) {
175
- if (i == j) continue;
176
- if (check(t[ i] , t[ j] )) {
169
+ for (int j = 0; j < n && ok; ++j) {
170
+ if (i != j && check(nums[i], nums[j])) {
177
171
ok = false;
178
- break;
179
172
}
180
173
}
181
174
if (ok) {
182
175
ans.push_back(i);
183
176
}
184
177
}
185
- return ans;
186
- }
187
178
188
- bool check(unordered_set<int>& nums1, unordered_set<int>& nums2) {
189
- for (int v : nums1) {
190
- if (!nums2.count(v)) {
191
- return false;
192
- }
193
- }
194
- return true ;
179
+ return ans;
195
180
}
196
181
};
197
182
```
198
183
199
184
#### Go
200
185
201
186
``` go
202
- func peopleIndexes (favoriteCompanies [][]string ) []int {
203
- d := map [string ]int {}
204
- idx , n := 0 , len (favoriteCompanies)
205
- t := make ([]map [int ]bool , n)
206
- for i , v := range favoriteCompanies {
207
- for _ , c := range v {
208
- if _ , ok := d[c]; !ok {
209
- d[c] = idx
187
+ func peopleIndexes (favoriteCompanies [][]string ) (ans []int ) {
188
+ n := len (favoriteCompanies)
189
+ d := make (map [string ]int )
190
+ idx := 0
191
+ nums := make ([]map [int ]struct {}, n)
192
+
193
+ for i := 0 ; i < n; i++ {
194
+ nums[i] = make (map [int ]struct {})
195
+ for _ , s := range favoriteCompanies[i] {
196
+ if _ , ok := d[s]; !ok {
197
+ d[s] = idx
210
198
idx++
211
199
}
200
+ nums[i][d[s]] = struct {}{}
212
201
}
213
- s := map [int ]bool {}
214
- for _ , c := range v {
215
- s[d[c]] = true
216
- }
217
- t[i] = s
218
202
}
219
- ans := [] int {}
220
- check := func (nums1, nums2 map [int ]bool ) bool {
221
- for v , _ := range nums1 {
222
- if _ , ok := nums2[v ]; !ok {
203
+
204
+ check := func (a, b map [int ]struct {} ) bool {
205
+ for x := range a {
206
+ if _ , ok := b[x ]; !ok {
223
207
return false
224
208
}
225
209
}
226
210
return true
227
211
}
228
212
for i := 0 ; i < n; i++ {
229
213
ok := true
230
- for j := 0 ; j < n; j++ {
231
- if i == j {
232
- continue
233
- }
234
- if check (t[i], t[j]) {
214
+ for j := 0 ; j < n && ok; j++ {
215
+ if i != j && check (nums[i], nums[j]) {
235
216
ok = false
236
- break
237
217
}
238
218
}
239
219
if ok {
240
220
ans = append (ans, i)
241
221
}
242
222
}
243
- return ans
223
+
224
+ return
225
+ }
226
+ ```
227
+
228
+ #### TypeScript
229
+
230
+ ``` ts
231
+ function peopleIndexes(favoriteCompanies : string [][]): number [] {
232
+ const n = favoriteCompanies .length ;
233
+ const d: Map <string , number > = new Map ();
234
+ let idx = 0 ;
235
+ const nums: Set <number >[] = Array .from ({ length: n }, () => new Set <number >());
236
+
237
+ for (let i = 0 ; i < n ; i ++ ) {
238
+ for (const s of favoriteCompanies [i ]) {
239
+ if (! d .has (s )) {
240
+ d .set (s , idx ++ );
241
+ }
242
+ nums [i ].add (d .get (s )! );
243
+ }
244
+ }
245
+
246
+ const check = (a : Set <number >, b : Set <number >): boolean => {
247
+ for (const x of a ) {
248
+ if (! b .has (x )) {
249
+ return false ;
250
+ }
251
+ }
252
+ return true ;
253
+ };
254
+
255
+ const ans: number [] = [];
256
+ for (let i = 0 ; i < n ; i ++ ) {
257
+ let ok = true ;
258
+ for (let j = 0 ; j < n && ok ; j ++ ) {
259
+ if (i !== j && check (nums [i ], nums [j ])) {
260
+ ok = false ;
261
+ }
262
+ }
263
+ if (ok ) {
264
+ ans .push (i );
265
+ }
266
+ }
267
+
268
+ return ans ;
244
269
}
245
270
```
246
271
0 commit comments