Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of People to Teach

med
30 min
Try to solve the Minimum Number of People to Teach problem.

Statement

There are nn languages numbered from 11 to nn, and mm users numbered from 11 to mm.

You are given an integer nn, a 22D integer array languages where languages[i] is the list of languages known by the (i+1)extth(i + 1)^{ ext{th}} user, and a 22D integer array friendships where friendships[j] = [uju_j, vjv_j] denotes a friendship between user uju_j and user vjv_j.

Two friends can communicate if there is at least one language that both of them know. You can choose exactly one language and teach it to some users.

Return the minimum number of users you need to teach that chosen language to ensure that every pair of friends can communicate.

Constraints:

  • 2≤n≤5002 \leq n \leq 500

  • languages.length ==m== m

  • 1≤m≤5001 \leq m \leq 500

  • 1≤1 \leq languages[i].length ≤n\leq n

  • 1≤1 \leq languages[i][j] ≤n\leq n

  • 1≤ui<vi≤1 \leq u_i < v_i \leq languages.length

  • 1≤1 \leq friendships.length ≤500\leq 500

  • All pairs (ui,vi)(u_i, v_i) are unique

  • languages[i] contains only unique values

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of People to Teach

med
30 min
Try to solve the Minimum Number of People to Teach problem.

Statement

There are nn languages numbered from 11 to nn, and mm users numbered from 11 to mm.

You are given an integer nn, a 22D integer array languages where languages[i] is the list of languages known by the (i+1)extth(i + 1)^{ ext{th}} user, and a 22D integer array friendships where friendships[j] = [uju_j, vjv_j] denotes a friendship between user uju_j and user vjv_j.

Two friends can communicate if there is at least one language that both of them know. You can choose exactly one language and teach it to some users.

Return the minimum number of users you need to teach that chosen language to ensure that every pair of friends can communicate.

Constraints:

  • 2≤n≤5002 \leq n \leq 500

  • languages.length ==m== m

  • 1≤m≤5001 \leq m \leq 500

  • 1≤1 \leq languages[i].length ≤n\leq n

  • 1≤1 \leq languages[i][j] ≤n\leq n

  • 1≤ui<vi≤1 \leq u_i < v_i \leq languages.length

  • 1≤1 \leq friendships.length ≤500\leq 500

  • All pairs (ui,vi)(u_i, v_i) are unique

  • languages[i] contains only unique values