Skip to content

Commit 11e2cae

Browse files
authored
Merge pull request #1309 from 0xff-dev/1733
Add solution and test-cases for problem 1733
2 parents c936d1f + d616bf2 commit 11e2cae

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1733.Minimum Number of People to Teach][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
On a social network consisting of `m` users and some friendships between users, two users can communicate with each other if they know a common language.
5+
6+
You are given an integer `n`, an array `languages`, and an array `friendships` where:
7+
8+
- There are `n` languages numbered `1` through `n`,
9+
- `languages[i]` is the set of languages the `ith` user knows, and
10+
- `friendships[i] = [ui, vi]` denotes a friendship between the users `ui` and `vi`.
11+
12+
You can choose **one** language and teach it to some users so that all friends can communicate with each other. Return the **minimum** number of users you need to teach.
13+
14+
Note that friendships are not transitive, meaning if `x` is a friend of `y` and `y` is a friend of `z`, this doesn't guarantee that `x` is a friend of `z`.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: n = 2, languages = [[1],[2],[1,2]], friendships = [[1,2],[1,3],[2,3]]
20+
Output: 1
21+
Explanation: You can either teach user 1 the second language or user 2 the first language.
1322
```
1423

15-
## 题意
16-
> ...
17-
18-
## 题解
24+
**Example 2:**
1925

20-
### 思路1
21-
> ...
22-
Minimum Number of People to Teach
23-
```go
2426
```
25-
27+
Input: n = 3, languages = [[2],[1,3],[1,2],[3]], friendships = [[1,4],[1,2],[3,4],[2,3]]
28+
Output: 2
29+
Explanation: Teach the third language to users 1 and 3, yielding two users to teach.
30+
```
2631

2732
## 结语
2833

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(n int, languages [][]int, friendships [][]int) int {
4+
cncon := make(map[int]bool)
5+
for _, friendship := range friendships {
6+
mp := make(map[int]bool)
7+
conm := false
8+
for _, lan := range languages[friendship[0]-1] {
9+
mp[lan] = true
10+
}
11+
for _, lan := range languages[friendship[1]-1] {
12+
if mp[lan] {
13+
conm = true
14+
break
15+
}
16+
}
17+
if !conm {
18+
cncon[friendship[0]-1] = true
19+
cncon[friendship[1]-1] = true
20+
}
21+
}
22+
23+
maxCnt := 0
24+
cnt := make([]int, n+1)
25+
for person := range cncon {
26+
for _, lan := range languages[person] {
27+
cnt[lan]++
28+
maxCnt = max(maxCnt, cnt[lan])
29+
}
30+
}
31+
32+
return len(cncon) - maxCnt
533
}

leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n int
14+
languages, friendships [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 2, [][]int{{1}, {2}, {1, 2}}, [][]int{{1, 2}, {1, 3}, {2, 3}}, 1},
18+
{"TestCase2", 3, [][]int{{2}, {1, 3}, {1, 2}, {3}}, [][]int{{1, 4}, {1, 2}, {3, 4}, {2, 3}}, 2},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.n, c.languages, c.friendships)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.n, c.languages, c.friendships)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)