From d616bf2318d52937c47421e0df65f452b0c11353 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 10 Sep 2025 09:07:59 +0800 Subject: [PATCH] Add solution and test-cases for problem 1733 --- .../README.md | 33 +++++++++++-------- .../Solution.go | 32 ++++++++++++++++-- .../Solution_test.go | 22 ++++++------- 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/README.md b/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/README.md index 18fada36e..33f9c9089 100755 --- a/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/README.md +++ b/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution.go b/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution.go index d115ccf5e..22e84f671 100644 --- a/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution.go +++ b/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution.go @@ -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 } diff --git a/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution_test.go b/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution_test.go index 14ff50eb4..8439db655 100644 --- a/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution_test.go +++ b/leetcode/1701-1800/1733.Minimum-Number-of-People-to-Teach/Solution_test.go @@ -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() { }