Skip to content

Commit

Permalink
Merge pull request #794 from 0xff-dev/2092
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2092
  • Loading branch information
6boris committed Mar 23, 2024
2 parents a36dc28 + 6722b33 commit a0dda26
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 26 deletions.
49 changes: 36 additions & 13 deletions leetcode/2001-2100/2092.Find-All-People-With-Secret/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
# [2092.Find All People With Secret][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You are given an integer `n` indicating there are `n` people numbered from `0` to `n - 1`. You are also given a **0-indexed** 2D integer array `meetings` where `meetings[i] = [xi, yi, timei]` indicates that person `xi` and person `yi` have a meeting at `timei`. A person may attend **multiple meetings** at the same time. Finally, you are given an integer `firstPerson`.

Person `0` has a **secret** and initially shares the secret with a person `firstPerson` at time `0`. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person `xi` has the secret at `timei`, then they will share the secret with person `yi`, and vice versa.

The secrets are shared **instantaneously**. That is, a person may receive the secret and share it with people in other meetings within the same time frame.

Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in **any order**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
Output: [0,1,2,3,5]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 5, person 1 shares the secret with person 2.
At time 8, person 2 shares the secret with person 3.
At time 10, person 1 shares the secret with person 5.​​​​
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Find All People With Secret
```go
```
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
Output: [0,1,3]
Explanation:
At time 0, person 0 shares the secret with person 3.
At time 2, neither person 1 nor person 2 know the secret.
At time 3, person 3 shares the secret with person 0 and person 1.
Thus, people 0, 1, and 3 know the secret after all the meetings.
```

**Example 3:**

```
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
Output: [0,1,2,3,4]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
Note that person 2 can share the secret at the same time as receiving it.
At time 2, person 3 shares the secret with person 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
```

## 结语

Expand Down
78 changes: 76 additions & 2 deletions leetcode/2001-2100/2092.Find-All-People-With-Secret/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,79 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

type unionFinc2092 struct {
father []int
}

func (u *unionFinc2092) find(x int) int {
if u.father[x] != x {
u.father[x] = u.find(u.father[x])
}
return u.father[x]
}

func (u *unionFinc2092) union(x, y int) {
fx := u.find(x)
fy := u.find(y)
if fx < fy {
u.father[fy] = fx
return
}
u.father[fx] = fy
}

func Solution(n int, meetings [][]int, firstPerson int) []int {
sort.Slice(meetings, func(i, j int) bool {
return meetings[i][2] < meetings[j][2]
})

u := unionFinc2092{father: make([]int, n)}
for i := 0; i < n; i++ {
u.father[i] = i
}
u.father[firstPerson] = 0
startTime := -1
for cur, meet := range meetings {
if meet[2] == startTime {
u.union(meet[0], meet[1])
continue
}
for i := cur - 1; i >= 0; i-- {
if meetings[i][2] != startTime {
break
}
fx := u.find(meetings[i][0])
fy := u.find(meetings[i][1])
if fx == 0 || fy == 0 {
u.union(meetings[i][0], meetings[i][1])
} else {
u.father[meetings[i][0]] = meetings[i][0]
u.father[meetings[i][1]] = meetings[i][1]
}
}

startTime = meet[2]
u.union(meet[0], meet[1])
}
for i := len(meetings) - 1; i >= 0; i-- {
if meetings[i][2] != startTime {
break
}
fx := u.find(meetings[i][0])
fy := u.find(meetings[i][1])
if fx == 0 || fy == 0 {
u.union(meetings[i][0], meetings[i][1])
} else {
u.father[meetings[i][0]] = meetings[i][0]
u.father[meetings[i][1]] = meetings[i][1]
}
}
ans := make([]int, 0)
for i := 0; i < n; i++ {
if u.find(i) == 0 {
ans = append(ans, i)
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,39 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
n int
meetings [][]int
firstPerson int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 6, [][]int{
{1, 2, 5}, {2, 3, 8}, {1, 5, 10},
}, 1, []int{0, 1, 2, 3, 5}},
{"TestCase2", 4, [][]int{
{3, 1, 3}, {1, 2, 2}, {0, 3, 3},
}, 3, []int{0, 1, 3}},
{"TestCase3", 5, [][]int{
{3, 4, 2}, {1, 2, 1}, {2, 3, 1},
}, 1, []int{0, 1, 2, 3, 4}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.meetings, c.firstPerson)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.n, c.meetings, c.firstPerson)
}
})
}
}

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

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

0 comments on commit a0dda26

Please sign in to comment.