Skip to content

Commit b1c04d4

Browse files
authored
Merge pull request 6boris#1286 from 0xff-dev/1452
Add solution and test-cases for problem 1452
2 parents 8d541f3 + 98de3d9 commit b1c04d4

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [1452.People Whose List of Favorite Companies Is Not a Subset of Another List][title]
2+
3+
## Description
4+
Given the array `favoriteCompanies` where `favoriteCompanies[i]` is the list of favorites companies for the `ith` person (**indexed from 0**).
5+
6+
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.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]]
12+
Output: [0,1,4]
13+
Explanation:
14+
Person with index=2 has favoriteCompanies[2]=["google","facebook"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] corresponding to the person with index 0.
15+
Person with index=3 has favoriteCompanies[3]=["google"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] and favoriteCompanies[1]=["google","microsoft"].
16+
Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]]
23+
Output: [0,1]
24+
Explanation: In this case favoriteCompanies[2]=["facebook","google"] is a subset of favoriteCompanies[0]=["leetcode","google","facebook"], therefore, the answer is [0,1].
25+
```
26+
27+
**Example 3:**
28+
29+
```
30+
Input: favoriteCompanies = [["leetcode"],["google"],["facebook"],["amazon"]]
31+
Output: [0,1,2,3]
32+
```
33+
34+
## 结语
35+
36+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
37+
38+
[title]: https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/
39+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(favoriteCompanies [][]string) []int {
4+
l := len(favoriteCompanies)
5+
subMaps := make([]map[string]struct{}, l)
6+
for i := range favoriteCompanies {
7+
subMaps[i] = map[string]struct{}{}
8+
for _, com := range favoriteCompanies[i] {
9+
subMaps[i][com] = struct{}{}
10+
}
11+
}
12+
var ans []int
13+
for i := 0; i < l; i++ {
14+
ok := true
15+
for next := 0; next < l && ok; next++ {
16+
if next == i || len(subMaps[i]) > len(subMaps[next]) {
17+
continue
18+
}
19+
in := true
20+
for k := range subMaps[i] {
21+
if _, ok1 := subMaps[next][k]; !ok1 {
22+
in = false
23+
break
24+
}
25+
}
26+
ok = !in
27+
}
28+
if ok {
29+
ans = append(ans, i)
30+
}
31+
}
32+
return ans
533
}

leetcode/1401-1500/1452.People-Whose-List-of-Favorite-Companies-Is-Not-a-Subset-of-Another-List/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]string
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]string{{"leetcode", "google", "facebook"}, {"google", "microsoft"}, {"google", "facebook"}, {"google"}, {"amazon"}}, []int{0, 1, 4}},
17+
{"TestCase2", [][]string{{"leetcode", "google", "facebook"}, {"leetcode", "amazon"}, {"facebook", "google"}}, []int{0, 1}},
18+
{"TestCase3", [][]string{{"leetcode"}, {"google"}, {"facebook"}, {"amazon"}}, []int{0, 1, 2, 3}},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)