Skip to content

Commit e133b4e

Browse files
authoredAug 3, 2024··
feat: add solutions to lc problem: No.1452 (#3357)
No.1452.People Whose List of Favorite Companies Is Not a Subset of Another List
1 parent 6a9164e commit e133b4e

File tree

7 files changed

+330
-256
lines changed

7 files changed

+330
-256
lines changed
 

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

Lines changed: 110 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tags:
2929
<p><strong>示例 1:</strong></p>
3030

3131
<pre><strong>输入:</strong>favoriteCompanies = [[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;],[&quot;google&quot;,&quot;microsoft&quot;],[&quot;google&quot;,&quot;facebook&quot;],[&quot;google&quot;],[&quot;amazon&quot;]]
32-
<strong>输出:</strong>[0,1,4]
32+
<strong>输出:</strong>[0,1,4]
3333
<strong>解释:</strong>
3434
favoriteCompanies[2]=[&quot;google&quot;,&quot;facebook&quot;] 是 favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] 的子集。
3535
favoriteCompanies[3]=[&quot;google&quot;] 是 favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] 和 favoriteCompanies[1]=[&quot;google&quot;,&quot;microsoft&quot;] 的子集。
@@ -39,7 +39,7 @@ favoriteCompanies[3]=[&quot;google&quot;] 是 favoriteCompanies[0]=[&quot;leetco
3939
<p><strong>示例 2:</strong></p>
4040

4141
<pre><strong>输入:</strong>favoriteCompanies = [[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;],[&quot;leetcode&quot;,&quot;amazon&quot;],[&quot;facebook&quot;,&quot;google&quot;]]
42-
<strong>输出:</strong>[0,1]
42+
<strong>输出:</strong>[0,1]
4343
<strong>解释:</strong>favoriteCompanies[2]=[&quot;facebook&quot;,&quot;google&quot;] 是 favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] 的子集,因此,答案为 [0,1] 。
4444
</pre>
4545

@@ -70,9 +70,9 @@ favoriteCompanies[3]=[&quot;google&quot;] 是 favoriteCompanies[0]=[&quot;leetco
7070

7171
### 方法一:哈希表
7272

73-
将每个 `company` 字符串列表都转换为一个整数类型的集合。然后遍历每个集合,判断其是否是其他集合的子集,如果不是,则将其下标加入结果集
73+
我们可以将每个公司映射到一个唯一的整数,然后对于每个人,我们将他们收藏的公司转换为整数集合,最后判断是否存在一个人的收藏公司是另一个人的子集
7474

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$ 是每个公司的平均长度
7676

7777
<!-- tabs:start -->
7878

@@ -81,25 +81,19 @@ favoriteCompanies[3]=[&quot;google&quot;] 是 favoriteCompanies[0]=[&quot;leetco
8181
```python
8282
class Solution:
8383
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
84-
d = {}
8584
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
9192
idx += 1
92-
t.append({d[c] for c in v})
93+
nums[i].add(d[s])
9394
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)):
10397
ans.append(i)
10498
return ans
10599
```
@@ -109,32 +103,26 @@ class Solution:
109103
```java
110104
class Solution {
111105
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
106+
int n = favoriteCompanies.size();
112107
Map<String, Integer> d = new HashMap<>();
113108
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<>());
116111
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++);
121116
}
117+
nums[i].add(d.get(s));
122118
}
123-
Set<Integer> s = new HashSet<>();
124-
for (var c : v) {
125-
s.add(d.get(c));
126-
}
127-
t[i] = s;
128119
}
129120
List<Integer> ans = new ArrayList<>();
130121
for (int i = 0; i < n; ++i) {
131122
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;
138126
}
139127
}
140128
if (ok) {
@@ -152,95 +140,132 @@ class Solution {
152140
class Solution {
153141
public:
154142
vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
143+
int n = favoriteCompanies.size();
155144
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+
158148
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++;
163152
}
153+
nums[i].insert(d[s]);
164154
}
165-
unordered_set<int> s;
166-
for (auto& c : v) {
167-
s.insert(d[c]);
168-
}
169-
t[i] = s;
170155
}
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+
171166
vector<int> ans;
172167
for (int i = 0; i < n; ++i) {
173168
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])) {
177171
ok = false;
178-
break;
179172
}
180173
}
181174
if (ok) {
182175
ans.push_back(i);
183176
}
184177
}
185-
return ans;
186-
}
187178

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;
195180
}
196181
};
197182
```
198183

199184
#### Go
200185

201186
```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
210198
idx++
211199
}
200+
nums[i][d[s]] = struct{}{}
212201
}
213-
s := map[int]bool{}
214-
for _, c := range v {
215-
s[d[c]] = true
216-
}
217-
t[i] = s
218202
}
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 {
223207
return false
224208
}
225209
}
226210
return true
227211
}
228212
for i := 0; i < n; i++ {
229213
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]) {
235216
ok = false
236-
break
237217
}
238218
}
239219
if ok {
240220
ans = append(ans, i)
241221
}
242222
}
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;
244269
}
245270
```
246271

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

Lines changed: 116 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ tags:
2929

3030
<pre>
3131
<strong>Input:</strong> favoriteCompanies = [[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;],[&quot;google&quot;,&quot;microsoft&quot;],[&quot;google&quot;,&quot;facebook&quot;],[&quot;google&quot;],[&quot;amazon&quot;]]
32-
<strong>Output:</strong> [0,1,4]
33-
<strong>Explanation:</strong>
34-
Person with index=2 has favoriteCompanies[2]=[&quot;google&quot;,&quot;facebook&quot;] which is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] corresponding to the person with index 0.
35-
Person with index=3 has favoriteCompanies[3]=[&quot;google&quot;] which is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] and favoriteCompanies[1]=[&quot;google&quot;,&quot;microsoft&quot;].
32+
<strong>Output:</strong> [0,1,4]
33+
<strong>Explanation:</strong>
34+
Person with index=2 has favoriteCompanies[2]=[&quot;google&quot;,&quot;facebook&quot;] which is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] corresponding to the person with index 0.
35+
Person with index=3 has favoriteCompanies[3]=[&quot;google&quot;] which is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;] and favoriteCompanies[1]=[&quot;google&quot;,&quot;microsoft&quot;].
3636
Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].
3737
</pre>
3838

3939
<p><strong class="example">Example 2:</strong></p>
4040

4141
<pre>
4242
<strong>Input:</strong> favoriteCompanies = [[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;],[&quot;leetcode&quot;,&quot;amazon&quot;],[&quot;facebook&quot;,&quot;google&quot;]]
43-
<strong>Output:</strong> [0,1]
43+
<strong>Output:</strong> [0,1]
4444
<strong>Explanation:</strong> In this case favoriteCompanies[2]=[&quot;facebook&quot;,&quot;google&quot;] is a subset of favoriteCompanies[0]=[&quot;leetcode&quot;,&quot;google&quot;,&quot;facebook&quot;], therefore, the answer is [0,1].
4545
</pre>
4646

@@ -69,7 +69,11 @@ Other lists of favorite companies are not a subset of another list, therefore, t
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Hash Table
73+
74+
We can map each company to a unique integer. Then, for each person, we convert their favorite companies into a set of integers. Finally, we check if the favorite companies of one person are a subset of another person's favorite companies.
75+
76+
The time complexity is $(n \times m \times k + n^2 \times m)$, and the space complexity is $O(n \times m)$. Here, $n$ and $m$ are the lengths of `favoriteCompanies` and the average length of each company's list, respectively, and $k$ is the average length of each company.
7377

7478
<!-- tabs:start -->
7579

@@ -78,25 +82,19 @@ Other lists of favorite companies are not a subset of another list, therefore, t
7882
```python
7983
class Solution:
8084
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
81-
d = {}
8285
idx = 0
83-
t = []
84-
for v in favoriteCompanies:
85-
for c in v:
86-
if c not in d:
87-
d[c] = idx
86+
d = {}
87+
n = len(favoriteCompanies)
88+
nums = [set() for _ in range(n)]
89+
for i, ss in enumerate(favoriteCompanies):
90+
for s in ss:
91+
if s not in d:
92+
d[s] = idx
8893
idx += 1
89-
t.append({d[c] for c in v})
94+
nums[i].add(d[s])
9095
ans = []
91-
for i, nums1 in enumerate(t):
92-
ok = True
93-
for j, nums2 in enumerate(t):
94-
if i == j:
95-
continue
96-
if not (nums1 - nums2):
97-
ok = False
98-
break
99-
if ok:
96+
for i in range(n):
97+
if not any(i != j and (nums[i] & nums[j]) == nums[i] for j in range(n)):
10098
ans.append(i)
10199
return ans
102100
```
@@ -106,32 +104,26 @@ class Solution:
106104
```java
107105
class Solution {
108106
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
107+
int n = favoriteCompanies.size();
109108
Map<String, Integer> d = new HashMap<>();
110109
int idx = 0;
111-
int n = favoriteCompanies.size();
112-
Set<Integer>[] t = new Set[n];
110+
Set<Integer>[] nums = new Set[n];
111+
Arrays.setAll(nums, i -> new HashSet<>());
113112
for (int i = 0; i < n; ++i) {
114-
var v = favoriteCompanies.get(i);
115-
for (var c : v) {
116-
if (!d.containsKey(c)) {
117-
d.put(c, idx++);
113+
var ss = favoriteCompanies.get(i);
114+
for (var s : ss) {
115+
if (!d.containsKey(s)) {
116+
d.put(s, idx++);
118117
}
118+
nums[i].add(d.get(s));
119119
}
120-
Set<Integer> s = new HashSet<>();
121-
for (var c : v) {
122-
s.add(d.get(c));
123-
}
124-
t[i] = s;
125120
}
126121
List<Integer> ans = new ArrayList<>();
127122
for (int i = 0; i < n; ++i) {
128123
boolean ok = true;
129-
for (int j = 0; j < n; ++j) {
130-
if (i != j) {
131-
if (t[j].containsAll(t[i])) {
132-
ok = false;
133-
break;
134-
}
124+
for (int j = 0; j < n && ok; ++j) {
125+
if (i != j && nums[j].containsAll(nums[i])) {
126+
ok = false;
135127
}
136128
}
137129
if (ok) {
@@ -149,95 +141,132 @@ class Solution {
149141
class Solution {
150142
public:
151143
vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
144+
int n = favoriteCompanies.size();
152145
unordered_map<string, int> d;
153-
int idx = 0, n = favoriteCompanies.size();
154-
vector<unordered_set<int>> t(n);
146+
int idx = 0;
147+
vector<unordered_set<int>> nums(n);
148+
155149
for (int i = 0; i < n; ++i) {
156-
auto v = favoriteCompanies[i];
157-
for (auto& c : v) {
158-
if (!d.count(c)) {
159-
d[c] = idx++;
150+
for (const auto& s : favoriteCompanies[i]) {
151+
if (!d.contains(s)) {
152+
d[s] = idx++;
160153
}
154+
nums[i].insert(d[s]);
161155
}
162-
unordered_set<int> s;
163-
for (auto& c : v) {
164-
s.insert(d[c]);
165-
}
166-
t[i] = s;
167156
}
157+
158+
auto check = [](const unordered_set<int>& a, const unordered_set<int>& b) {
159+
for (int x : a) {
160+
if (!b.contains(x)) {
161+
return false;
162+
}
163+
}
164+
return true;
165+
};
166+
168167
vector<int> ans;
169168
for (int i = 0; i < n; ++i) {
170169
bool ok = true;
171-
for (int j = 0; j < n; ++j) {
172-
if (i == j) continue;
173-
if (check(t[i], t[j])) {
170+
for (int j = 0; j < n && ok; ++j) {
171+
if (i != j && check(nums[i], nums[j])) {
174172
ok = false;
175-
break;
176173
}
177174
}
178175
if (ok) {
179176
ans.push_back(i);
180177
}
181178
}
182-
return ans;
183-
}
184179

185-
bool check(unordered_set<int>& nums1, unordered_set<int>& nums2) {
186-
for (int v : nums1) {
187-
if (!nums2.count(v)) {
188-
return false;
189-
}
190-
}
191-
return true;
180+
return ans;
192181
}
193182
};
194183
```
195184

196185
#### Go
197186

198187
```go
199-
func peopleIndexes(favoriteCompanies [][]string) []int {
200-
d := map[string]int{}
201-
idx, n := 0, len(favoriteCompanies)
202-
t := make([]map[int]bool, n)
203-
for i, v := range favoriteCompanies {
204-
for _, c := range v {
205-
if _, ok := d[c]; !ok {
206-
d[c] = idx
188+
func peopleIndexes(favoriteCompanies [][]string) (ans []int) {
189+
n := len(favoriteCompanies)
190+
d := make(map[string]int)
191+
idx := 0
192+
nums := make([]map[int]struct{}, n)
193+
194+
for i := 0; i < n; i++ {
195+
nums[i] = make(map[int]struct{})
196+
for _, s := range favoriteCompanies[i] {
197+
if _, ok := d[s]; !ok {
198+
d[s] = idx
207199
idx++
208200
}
201+
nums[i][d[s]] = struct{}{}
209202
}
210-
s := map[int]bool{}
211-
for _, c := range v {
212-
s[d[c]] = true
213-
}
214-
t[i] = s
215203
}
216-
ans := []int{}
217-
check := func(nums1, nums2 map[int]bool) bool {
218-
for v, _ := range nums1 {
219-
if _, ok := nums2[v]; !ok {
204+
205+
check := func(a, b map[int]struct{}) bool {
206+
for x := range a {
207+
if _, ok := b[x]; !ok {
220208
return false
221209
}
222210
}
223211
return true
224212
}
225213
for i := 0; i < n; i++ {
226214
ok := true
227-
for j := 0; j < n; j++ {
228-
if i == j {
229-
continue
230-
}
231-
if check(t[i], t[j]) {
215+
for j := 0; j < n && ok; j++ {
216+
if i != j && check(nums[i], nums[j]) {
232217
ok = false
233-
break
234218
}
235219
}
236220
if ok {
237221
ans = append(ans, i)
238222
}
239223
}
240-
return ans
224+
225+
return
226+
}
227+
```
228+
229+
#### TypeScript
230+
231+
```ts
232+
function peopleIndexes(favoriteCompanies: string[][]): number[] {
233+
const n = favoriteCompanies.length;
234+
const d: Map<string, number> = new Map();
235+
let idx = 0;
236+
const nums: Set<number>[] = Array.from({ length: n }, () => new Set<number>());
237+
238+
for (let i = 0; i < n; i++) {
239+
for (const s of favoriteCompanies[i]) {
240+
if (!d.has(s)) {
241+
d.set(s, idx++);
242+
}
243+
nums[i].add(d.get(s)!);
244+
}
245+
}
246+
247+
const check = (a: Set<number>, b: Set<number>): boolean => {
248+
for (const x of a) {
249+
if (!b.has(x)) {
250+
return false;
251+
}
252+
}
253+
return true;
254+
};
255+
256+
const ans: number[] = [];
257+
for (let i = 0; i < n; i++) {
258+
let ok = true;
259+
for (let j = 0; j < n && ok; j++) {
260+
if (i !== j && check(nums[i], nums[j])) {
261+
ok = false;
262+
}
263+
}
264+
if (ok) {
265+
ans.push(i);
266+
}
267+
}
268+
269+
return ans;
241270
}
242271
```
243272

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
class Solution {
22
public:
33
vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
4+
int n = favoriteCompanies.size();
45
unordered_map<string, int> d;
5-
int idx = 0, n = favoriteCompanies.size();
6-
vector<unordered_set<int>> t(n);
6+
int idx = 0;
7+
vector<unordered_set<int>> nums(n);
8+
79
for (int i = 0; i < n; ++i) {
8-
auto v = favoriteCompanies[i];
9-
for (auto& c : v) {
10-
if (!d.count(c)) {
11-
d[c] = idx++;
10+
for (const auto& s : favoriteCompanies[i]) {
11+
if (!d.contains(s)) {
12+
d[s] = idx++;
1213
}
14+
nums[i].insert(d[s]);
1315
}
14-
unordered_set<int> s;
15-
for (auto& c : v) {
16-
s.insert(d[c]);
17-
}
18-
t[i] = s;
1916
}
17+
18+
auto check = [](const unordered_set<int>& a, const unordered_set<int>& b) {
19+
for (int x : a) {
20+
if (!b.contains(x)) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
};
26+
2027
vector<int> ans;
2128
for (int i = 0; i < n; ++i) {
2229
bool ok = true;
23-
for (int j = 0; j < n; ++j) {
24-
if (i == j) continue;
25-
if (check(t[i], t[j])) {
30+
for (int j = 0; j < n && ok; ++j) {
31+
if (i != j && check(nums[i], nums[j])) {
2632
ok = false;
27-
break;
2833
}
2934
}
3035
if (ok) {
3136
ans.push_back(i);
3237
}
3338
}
34-
return ans;
35-
}
3639

37-
bool check(unordered_set<int>& nums1, unordered_set<int>& nums2) {
38-
for (int v : nums1) {
39-
if (!nums2.count(v)) {
40-
return false;
41-
}
42-
}
43-
return true;
40+
return ans;
4441
}
45-
};
42+
};
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
1-
func peopleIndexes(favoriteCompanies [][]string) []int {
2-
d := map[string]int{}
3-
idx, n := 0, len(favoriteCompanies)
4-
t := make([]map[int]bool, n)
5-
for i, v := range favoriteCompanies {
6-
for _, c := range v {
7-
if _, ok := d[c]; !ok {
8-
d[c] = idx
1+
func peopleIndexes(favoriteCompanies [][]string) (ans []int) {
2+
n := len(favoriteCompanies)
3+
d := make(map[string]int)
4+
idx := 0
5+
nums := make([]map[int]struct{}, n)
6+
7+
for i := 0; i < n; i++ {
8+
nums[i] = make(map[int]struct{})
9+
for _, s := range favoriteCompanies[i] {
10+
if _, ok := d[s]; !ok {
11+
d[s] = idx
912
idx++
1013
}
14+
nums[i][d[s]] = struct{}{}
1115
}
12-
s := map[int]bool{}
13-
for _, c := range v {
14-
s[d[c]] = true
15-
}
16-
t[i] = s
1716
}
18-
ans := []int{}
19-
check := func(nums1, nums2 map[int]bool) bool {
20-
for v, _ := range nums1 {
21-
if _, ok := nums2[v]; !ok {
17+
18+
check := func(a, b map[int]struct{}) bool {
19+
for x := range a {
20+
if _, ok := b[x]; !ok {
2221
return false
2322
}
2423
}
2524
return true
2625
}
2726
for i := 0; i < n; i++ {
2827
ok := true
29-
for j := 0; j < n; j++ {
30-
if i == j {
31-
continue
32-
}
33-
if check(t[i], t[j]) {
28+
for j := 0; j < n && ok; j++ {
29+
if i != j && check(nums[i], nums[j]) {
3430
ok = false
35-
break
3631
}
3732
}
3833
if ok {
3934
ans = append(ans, i)
4035
}
4136
}
42-
return ans
43-
}
37+
38+
return
39+
}
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
class Solution {
22
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
3+
int n = favoriteCompanies.size();
34
Map<String, Integer> d = new HashMap<>();
45
int idx = 0;
5-
int n = favoriteCompanies.size();
6-
Set<Integer>[] t = new Set[n];
6+
Set<Integer>[] nums = new Set[n];
7+
Arrays.setAll(nums, i -> new HashSet<>());
78
for (int i = 0; i < n; ++i) {
8-
var v = favoriteCompanies.get(i);
9-
for (var c : v) {
10-
if (!d.containsKey(c)) {
11-
d.put(c, idx++);
9+
var ss = favoriteCompanies.get(i);
10+
for (var s : ss) {
11+
if (!d.containsKey(s)) {
12+
d.put(s, idx++);
1213
}
14+
nums[i].add(d.get(s));
1315
}
14-
Set<Integer> s = new HashSet<>();
15-
for (var c : v) {
16-
s.add(d.get(c));
17-
}
18-
t[i] = s;
1916
}
2017
List<Integer> ans = new ArrayList<>();
2118
for (int i = 0; i < n; ++i) {
2219
boolean ok = true;
23-
for (int j = 0; j < n; ++j) {
24-
if (i != j) {
25-
if (t[j].containsAll(t[i])) {
26-
ok = false;
27-
break;
28-
}
20+
for (int j = 0; j < n && ok; ++j) {
21+
if (i != j && nums[j].containsAll(nums[i])) {
22+
ok = false;
2923
}
3024
}
3125
if (ok) {
@@ -34,4 +28,4 @@ public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
3428
}
3529
return ans;
3630
}
37-
}
31+
}
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
class Solution:
22
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
3-
d = {}
43
idx = 0
5-
t = []
6-
for v in favoriteCompanies:
7-
for c in v:
8-
if c not in d:
9-
d[c] = idx
4+
d = {}
5+
n = len(favoriteCompanies)
6+
nums = [set() for _ in range(n)]
7+
for i, ss in enumerate(favoriteCompanies):
8+
for s in ss:
9+
if s not in d:
10+
d[s] = idx
1011
idx += 1
11-
t.append({d[c] for c in v})
12+
nums[i].add(d[s])
1213
ans = []
13-
for i, nums1 in enumerate(t):
14-
ok = True
15-
for j, nums2 in enumerate(t):
16-
if i == j:
17-
continue
18-
if not (nums1 - nums2):
19-
ok = False
20-
break
21-
if ok:
14+
for i in range(n):
15+
if not any(i != j and (nums[i] & nums[j]) == nums[i] for j in range(n)):
2216
ans.append(i)
2317
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function peopleIndexes(favoriteCompanies: string[][]): number[] {
2+
const n = favoriteCompanies.length;
3+
const d: Map<string, number> = new Map();
4+
let idx = 0;
5+
const nums: Set<number>[] = Array.from({ length: n }, () => new Set<number>());
6+
7+
for (let i = 0; i < n; i++) {
8+
for (const s of favoriteCompanies[i]) {
9+
if (!d.has(s)) {
10+
d.set(s, idx++);
11+
}
12+
nums[i].add(d.get(s)!);
13+
}
14+
}
15+
16+
const check = (a: Set<number>, b: Set<number>): boolean => {
17+
for (const x of a) {
18+
if (!b.has(x)) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
};
24+
25+
const ans: number[] = [];
26+
for (let i = 0; i < n; i++) {
27+
let ok = true;
28+
for (let j = 0; j < n && ok; j++) {
29+
if (i !== j && check(nums[i], nums[j])) {
30+
ok = false;
31+
}
32+
}
33+
if (ok) {
34+
ans.push(i);
35+
}
36+
}
37+
38+
return ans;
39+
}

0 commit comments

Comments
 (0)
Please sign in to comment.