LeetCode 题解工作台

需要教语言的最少人数

在一个由 m 个用户组成的社交网络里,我们获取到一些用户之间的好友关系。两个用户之间可以相互沟通的条件是他们都掌握同一门语言。 给你一个整数 n ,数组 languages 和数组 friendships ,它们的含义如下: 总共有 n 种语言,编号从 1 到 n 。 languages[i] 是第…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

对于每个好友关系,如果两个人掌握的语言集合不相交,则需要教一门语言,使得两个人可以相互沟通,我们将这些人放入一个哈希集合 中。 然后在这个集合 中,统计每种语言掌握的人数,获取最大的人数,我们记为 ,那么答案就是 $|s| - mx$。其中 表示集合 的大小。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

在一个由 m 个用户组成的社交网络里,我们获取到一些用户之间的好友关系。两个用户之间可以相互沟通的条件是他们都掌握同一门语言。

给你一个整数 n ,数组 languages 和数组 friendships ,它们的含义如下:

  • 总共有 n 种语言,编号从 1 到 n 。
  • languages[i] 是第 i 位用户掌握的语言集合。
  • friendships[i] = [u​​​​​​i​​​, v​​​​​​i] 表示 u​​​​​​​​​​​i​​​​​ 和 vi 为好友关系。

你可以选择 一门 语言并教会一些用户,使得所有好友之间都可以相互沟通。请返回你 最少 需要教会多少名用户。

请注意,好友关系没有传递性,也就是说如果 x 和 y 是好友,且 y 和 z 是好友, x 和 z 不一定是好友。

 

示例 1:

输入:n = 2, languages = [[1],[2],[1,2]], friendships = [[1,2],[1,3],[2,3]]
输出:1
解释:你可以选择教用户 1 第二门语言,也可以选择教用户 2 第一门语言。

示例 2:

输入:n = 3, languages = [[2],[1,3],[1,2],[3]], friendships = [[1,4],[1,2],[3,4],[2,3]]
输出:2
解释:教用户 1 和用户 3 第三门语言,需要教 2 名用户。

 

提示:

  • 2 <= n <= 500
  • languages.length == m
  • 1 <= m <= 500
  • 1 <= languages[i].length <= n
  • 1 <= languages[i][j] <= n
  • 1 <= u​​​​​​i < v​​​​​​i <= languages.length
  • 1 <= friendships.length <= 500
  • 所有的好友关系 (u​​​​​i, v​​​​​​i) 都是唯一的。
  • languages[i] 中包含的值互不相同。
lightbulb

解题思路

方法一:模拟 + 统计

对于每个好友关系,如果两个人掌握的语言集合不相交,则需要教一门语言,使得两个人可以相互沟通,我们将这些人放入一个哈希集合 ss 中。

然后在这个集合 ss 中,统计每种语言掌握的人数,获取最大的人数,我们记为 mxmx,那么答案就是 smx|s| - mx。其中 s|s| 表示集合 ss 的大小。

时间复杂度 O(m2×k)O(m^2 \times k)。其中 mm 为语言的数量,而 kk 为好友关系的数量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
    def minimumTeachings(
        self, n: int, languages: List[List[int]], friendships: List[List[int]]
    ) -> int:
        def check(u: int, v: int) -> bool:
            for x in languages[u - 1]:
                for y in languages[v - 1]:
                    if x == y:
                        return True
            return False

        s = set()
        for u, v in friendships:
            if not check(u, v):
                s.add(u)
                s.add(v)
        cnt = Counter()
        for u in s:
            for l in languages[u - 1]:
                cnt[l] += 1
        return len(s) - max(cnt.values(), default=0)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidates should be able to identify the need for efficient lookups, especially with hash tables.

  • question_mark

    A good candidate will propose either the brute force or greedy approach and discuss trade-offs.

  • question_mark

    Look for understanding of greedy algorithms and optimization strategies when candidates discuss minimizing the number of users.

warning

常见陷阱

外企场景
  • error

    Focusing only on one approach without considering the trade-offs in terms of time complexity.

  • error

    Neglecting to handle users who already speak a common language, leading to over-counting the number of users to teach.

  • error

    Forgetting to account for edge cases, such as users who are isolated in the network and don't need any teaching.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Extend the problem to handle larger networks, requiring more advanced algorithms to scale efficiently.

  • arrow_right_alt

    Consider constraints where users can speak multiple languages and need to be taught multiple languages.

  • arrow_right_alt

    Add additional constraints, such as fixed language choices or a limited number of languages that can be taught.

help

常见问题

外企场景

需要教语言的最少人数题解:数组·哈希·扫描 | LeetCode #1733 中等