LeetCode: Minimum Number Of People To Teach Solution

1function intersection(setA, setB) {
2 let _intersection = new Set()
3 for (let elem of setB) {
4 if (setA.has(elem)) {
5 _intersection.add(elem)
6 }
7 }
8 return _intersection
9}
10
11var minimumTeachings = function (n, languages, friendships) {
12 languages = languages.map(l => new Set(l))
13 const conflictUsers = new Set()
14 for (let [i, j] of friendships) {
15 if (!intersection(languages[i - 1], languages[j - 1]).size) {
16 conflictUsers.add(i - 1)
17 conflictUsers.add(j - 1)
18 }
19 }
20 const languageCount = Array(n).fill(0)
21 for (const user of conflictUsers) {
22 for (const language of languages[user]) {
23 languageCount[language - 1]++
24 }
25 }
26 return conflictUsers.size - Math.max.apply(null, languageCount)
27}

Comments

Loading comments...

Tags

leetcode

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Find The Highest Altitude

Jan 24, 2021

Previous Post

HoningJS

Search Posts