Class Solution
java.lang.Object
g1701_1800.s1733_minimum_number_of_people_to_teach.Solution
1733 - Minimum Number of People to Teach.<p>Medium</p>
<p>On a social network consisting of <code>m</code> users and some friendships between users, two users can communicate with each other if they know a common language.</p>
<p>You are given an integer <code>n</code>, an array <code>languages</code>, and an array <code>friendships</code> where:</p>
<ul>
<li>There are <code>n</code> languages numbered <code>1</code> through <code>n</code>,</li>
<li><code>languages[i]</code> is the set of languages the <code>i<sup>th</sup></code> user knows, and</li>
<li><code>friendships[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a friendship between the users <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</li>
</ul>
<p>You can choose <strong>one</strong> language and teach it to some users so that all friends can communicate with each other. Return <em>the</em> <em><strong>minimum</strong></em> <em>number of users you need to teach.</em></p>
<p>Note that friendships are not transitive, meaning if <code>x</code> is a friend of <code>y</code> and <code>y</code> is a friend of <code>z</code>, this doesn’t guarantee that <code>x</code> is a friend of <code>z</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> n = 2, languages = [[1],[2],[1,2]], friendships = [[1,2],[1,3],[2,3]]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> You can either teach user 1 the second language or user 2 the first language.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> n = 3, languages = [[2],[1,3],[1,2],[3]], friendships = [[1,4],[1,2],[3,4],[2,3]]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> Teach the third language to users 1 and 3, yielding two users to teach.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 500</code></li>
<li><code>languages.length == m</code></li>
<li><code>1 <= m <= 500</code></li>
<li><code>1 <= languages[i].length <= n</code></li>
<li><code>1 <= languages[i][j] <= n</code></li>
<li><code>1 <= u<sub>i</sub> < v<sub>i</sub> <= languages.length</code></li>
<li><code>1 <= friendships.length <= 500</code></li>
<li>All tuples <code>(u<sub>i,</sub> v<sub>i</sub>)</code> are unique</li>
<li><code>languages[i]</code> contains only unique values</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
minimumTeachings
(int n, int[][] languages, int[][] friendships)
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minimumTeachings
public int minimumTeachings(int n, int[][] languages, int[][] friendships)
-