Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e133b4e

Browse files
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 = newSet[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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /