Skip to content

Add solution and test-cases for problem 1452 #1286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# [1452.People Whose List of Favorite Companies Is Not a Subset of Another List][title]

## Description
Given the array `favoriteCompanies` where `favoriteCompanies[i]` is the list of favorites companies for the `ith` person (**indexed from 0**).

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.

**Example 1:**

```
Input: favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]]
Output: [0,1,4]
Explanation:
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.
Person with index=3 has favoriteCompanies[3]=["google"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] and favoriteCompanies[1]=["google","microsoft"].
Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].
```

**Example 2:**

```
Input: favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]]
Output: [0,1]
Explanation: In this case favoriteCompanies[2]=["facebook","google"] is a subset of favoriteCompanies[0]=["leetcode","google","facebook"], therefore, the answer is [0,1].
```

**Example 3:**

```
Input: favoriteCompanies = [["leetcode"],["google"],["facebook"],["amazon"]]
Output: [0,1,2,3]
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(favoriteCompanies [][]string) []int {
l := len(favoriteCompanies)
subMaps := make([]map[string]struct{}, l)
for i := range favoriteCompanies {
subMaps[i] = map[string]struct{}{}
for _, com := range favoriteCompanies[i] {
subMaps[i][com] = struct{}{}
}
}
var ans []int
for i := 0; i < l; i++ {
ok := true
for next := 0; next < l && ok; next++ {
if next == i || len(subMaps[i]) > len(subMaps[next]) {
continue
}
in := true
for k := range subMaps[i] {
if _, ok1 := subMaps[next][k]; !ok1 {
in = false
break
}
}
ok = !in
}
if ok {
ans = append(ans, i)
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]string
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]string{{"leetcode", "google", "facebook"}, {"google", "microsoft"}, {"google", "facebook"}, {"google"}, {"amazon"}}, []int{0, 1, 4}},
{"TestCase2", [][]string{{"leetcode", "google", "facebook"}, {"leetcode", "amazon"}, {"facebook", "google"}}, []int{0, 1}},
{"TestCase3", [][]string{{"leetcode"}, {"google"}, {"facebook"}, {"amazon"}}, []int{0, 1, 2, 3}},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading