返回介绍

solution / 1700-1799 / 1733.Minimum Number of People to Teach / README_EN

发布于 2024-06-17 01:03:15 字数 6503 浏览 0 评论 0 收藏 0

1733. Minimum Number of People to Teach

中文文档

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 i​​​​​​th​​​​ user knows, and
  • friendships[i] = [u​​​​​​i​​​, v​​​​​​i] denotes a friendship between the users u​​​​​​​​​​​i​​​​​ 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: 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:

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.

 

Constraints:

  • 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
  • All tuples (u​​​​​i, v​​​​​​i) are unique
  • languages[i] contains only unique values

Solutions

Solution 1: Simulation + Statistics

For each friendship, if the sets of languages known by the two people do not intersect, then a language needs to be taught so that the two people can communicate with each other. We put these people into a hash set $s$.

Then in this set $s$, we count the number of people who know each language, and get the maximum number, which we denote as $mx$. So the answer is len(s) - mx.

The time complexity is $O(m^2 \times k)$. Here, $m$ is the number of languages, and $k$ is the number of friendships.

class Solution:
  def minimumTeachings(
    self, n: int, languages: List[List[int]], friendships: List[List[int]]
  ) -> int:
    def check(u, v):
      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)
class Solution {
  public int minimumTeachings(int n, int[][] languages, int[][] friendships) {
    Set<Integer> s = new HashSet<>();
    for (var e : friendships) {
      int u = e[0], v = e[1];
      if (!check(u, v, languages)) {
        s.add(u);
        s.add(v);
      }
    }
    if (s.isEmpty()) {
      return 0;
    }
    int[] cnt = new int[n + 1];
    for (int u : s) {
      for (int l : languages[u - 1]) {
        ++cnt[l];
      }
    }
    int mx = 0;
    for (int v : cnt) {
      mx = Math.max(mx, v);
    }
    return s.size() - mx;
  }

  private boolean check(int u, int v, int[][] languages) {
    for (int x : languages[u - 1]) {
      for (int y : languages[v - 1]) {
        if (x == y) {
          return true;
        }
      }
    }
    return false;
  }
}
class Solution {
public:
  int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {
    unordered_set<int> s;
    for (auto& e : friendships) {
      int u = e[0], v = e[1];
      if (!check(u, v, languages)) {
        s.insert(u);
        s.insert(v);
      }
    }
    if (s.empty()) {
      return 0;
    }
    vector<int> cnt(n + 1);
    for (int u : s) {
      for (int& l : languages[u - 1]) {
        ++cnt[l];
      }
    }
    return s.size() - *max_element(cnt.begin(), cnt.end());
  }

  bool check(int u, int v, vector<vector<int>>& languages) {
    for (int x : languages[u - 1]) {
      for (int y : languages[v - 1]) {
        if (x == y) {
          return true;
        }
      }
    }
    return false;
  }
};
func minimumTeachings(n int, languages [][]int, friendships [][]int) int {
  check := func(u, v int) bool {
    for _, x := range languages[u-1] {
      for _, y := range languages[v-1] {
        if x == y {
          return true
        }
      }
    }
    return false
  }
  s := map[int]bool{}
  for _, e := range friendships {
    u, v := e[0], e[1]
    if !check(u, v) {
      s[u], s[v] = true, true
    }
  }
  if len(s) == 0 {
    return 0
  }
  cnt := make([]int, n+1)
  for u := range s {
    for _, l := range languages[u-1] {
      cnt[l]++
    }
  }
  return len(s) - slices.Max(cnt)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文