Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c7e787d

Browse files
committed
Add solution #1733
1 parent a3e950c commit c7e787d

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,506 LeetCode solutions in JavaScript
1+
# 1,507 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1334,6 +1334,7 @@
13341334
1727|[Largest Submatrix With Rearrangements](./solutions/1727-largest-submatrix-with-rearrangements.js)|Medium|
13351335
1728|[Cat and Mouse II](./solutions/1728-cat-and-mouse-ii.js)|Hard|
13361336
1732|[Find the Highest Altitude](./solutions/1732-find-the-highest-altitude.js)|Easy|
1337+
1733|[Minimum Number of People to Teach](./solutions/1733-minimum-number-of-people-to-teach.js)|Medium|
13371338
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13381339
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13391340
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* 1733. Minimum Number of People to Teach
3+
* https://leetcode.com/problems/minimum-number-of-people-to-teach/
4+
* Difficulty: Medium
5+
*
6+
* On a social network consisting of m users and some friendships between users, two users can
7+
* communicate with each other if they know a common language.
8+
*
9+
* You are given an integer n, an array languages, and an array friendships where:
10+
* - There are n languages numbered 1 through n,
11+
* - languages[i] is the set of languages the ith user knows, and
12+
* - friendships[i] = [ui, vi] denotes a friendship between the users ui and vi.
13+
*
14+
* You can choose one language and teach it to some users so that all friends can communicate
15+
* with each other. Return the minimum number of users you need to teach.
16+
*
17+
* Note that friendships are not transitive, meaning if x is a friend of y and y is a friend of
18+
* z, this doesn't guarantee that x is a friend of z.
19+
*/
20+
21+
/**
22+
* @param {number} n
23+
* @param {number[][]} languages
24+
* @param {number[][]} friendships
25+
* @return {number}
26+
*/
27+
var minimumTeachings = function(n, languages, friendships) {
28+
const languageUsers = Array.from({ length: n + 1 }, () => new Set());
29+
const nonCommunicating = new Set();
30+
31+
for (let i = 0; i < languages.length; i++) {
32+
for (const lang of languages[i]) {
33+
languageUsers[lang].add(i + 1);
34+
}
35+
}
36+
37+
for (const [u, v] of friendships) {
38+
let canCommunicate = false;
39+
for (const lang of languages[u - 1]) {
40+
if (languages[v - 1].includes(lang)) {
41+
canCommunicate = true;
42+
break;
43+
}
44+
}
45+
if (!canCommunicate) {
46+
nonCommunicating.add(u);
47+
nonCommunicating.add(v);
48+
}
49+
}
50+
51+
let result = Infinity;
52+
for (let lang = 1; lang <= n; lang++) {
53+
let usersToTeach = 0;
54+
for (const user of nonCommunicating) {
55+
if (!languages[user - 1].includes(lang)) {
56+
usersToTeach++;
57+
}
58+
}
59+
result = Math.min(result, usersToTeach);
60+
}
61+
62+
return result;
63+
};

0 commit comments

Comments
 (0)