Skip to content

Commit d97b7a2

Browse files
authored
Update 2127-maximum-employees-to-be-invited-to-a-meeting.js
1 parent caa79de commit d97b7a2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

2127-maximum-employees-to-be-invited-to-a-meeting.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
/**
2+
* @param {number[]} favorite
3+
* @return {number}
4+
*/
5+
const maximumInvitations = function (favorite) {
6+
const n = favorite.length
7+
const indegree = Array(n).fill(0)
8+
for (let i = 0; i < n; i++) indegree[favorite[i]]++
9+
const { max } = Math
10+
let q = []
11+
const visited = Array(n).fill(0)
12+
const depth = Array(n).fill(1)
13+
for (let i = 0; i < n; i++) {
14+
if (indegree[i] === 0) {
15+
depth[i] = 1
16+
visited[i] = 1
17+
q.push(i)
18+
}
19+
}
20+
21+
while (q.length) {
22+
const cur = q.shift()
23+
const nxt = favorite[cur]
24+
indegree[nxt]--
25+
if (indegree[nxt] == 0) {
26+
q.push(nxt)
27+
visited[nxt] = 1
28+
}
29+
depth[nxt] = depth[cur] + 1
30+
}
31+
32+
let max_circle_size = 0
33+
let max_link_size = 0
34+
for (let i = 0; i < n; i++) {
35+
if (visited[i] === 1) continue
36+
let j = i
37+
let count = 0
38+
while (visited[j] == 0) {
39+
count++
40+
visited[j] = 1
41+
j = favorite[j]
42+
}
43+
if (count > 2) max_circle_size = max(max_circle_size, count)
44+
else if (count == 2) max_link_size += depth[i] + depth[favorite[i]]
45+
}
46+
47+
return max(max_circle_size, max_link_size)
48+
}
49+
50+
// another
51+
152
/**
253
* @param {number[]} favorite
354
* @return {number}

0 commit comments

Comments
 (0)