Skip to content
Merged
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
33 changes: 19 additions & 14 deletions leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# [1733.Minimum Number of People to Teach][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
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.

You are given an integer `n`, an array `languages`, and an array `friendships` where:

- There are `n` languages numbered `1` through `n`,
- `languages[i]` is the set of languages the `ith` user knows, and
- `friendships[i] = [ui, vi]` denotes a friendship between the users `ui` and `vi`.

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.

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`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 2, languages = [[1],[2],[1,2]], friendships = [[1,2],[1,3],[2,3]]
Output: 1
Explanation: You can either teach user 1 the second language or user 2 the first language.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Minimum Number of People to Teach
```go
```

Input: n = 3, languages = [[2],[1,3],[1,2],[3]], friendships = [[1,4],[1,2],[3,4],[2,3]]
Output: 2
Explanation: Teach the third language to users 1 and 3, yielding two users to teach.
```

## 结语

Expand Down
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(n int, languages [][]int, friendships [][]int) int {
cncon := make(map[int]bool)
for _, friendship := range friendships {
mp := make(map[int]bool)
conm := false
for _, lan := range languages[friendship[0]-1] {
mp[lan] = true
}
for _, lan := range languages[friendship[1]-1] {
if mp[lan] {
conm = true
break
}
}
if !conm {
cncon[friendship[0]-1] = true
cncon[friendship[1]-1] = true
}
}

maxCnt := 0
cnt := make([]int, n+1)
for person := range cncon {
for _, lan := range languages[person] {
cnt[lan]++
maxCnt = max(maxCnt, cnt[lan])
}
}

return len(cncon) - maxCnt
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
n int
languages, friendships [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, [][]int{{1}, {2}, {1, 2}}, [][]int{{1, 2}, {1, 3}, {2, 3}}, 1},
{"TestCase2", 3, [][]int{{2}, {1, 3}, {1, 2}, {3}}, [][]int{{1, 4}, {1, 2}, {3, 4}, {2, 3}}, 2},
}

// 开始测试
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.languages, c.friendships)
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.languages, c.friendships)
}
})
}
}

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

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