diff --git a/2127. Maximum Employees to Be Invited to a Meeting b/2127. Maximum Employees to Be Invited to a Meeting new file mode 100644 index 0000000..074792a --- /dev/null +++ b/2127. Maximum Employees to Be Invited to a Meeting @@ -0,0 +1,48 @@ +class Solution { +public: + int maximumInvitations(vector& favorite) { + + int n = favorite.size(); + vector indegree(n, 0); + vector chain(n, 0); + vector vis(n, false); + for (int i : favorite) { + indegree[i]++; + } + queue q; + for (int i = 0; i < n; i++) { + if (!indegree[i]) { + q.push(i); + } + } + while (!q.empty()) { + int front = q.front(); + q.pop(); + vis[front] = true; + int next = favorite[front]; + chain[next] = chain[front] + 1; + if (--indegree[next] == 0) { + q.push(next); + } + } + + int maxCycle = 0, total = 0; + for (int i = 0; i < n; i++) { + if (!vis[i]) { + int c = i, len = 0; + while (!vis[c]) { + vis[c] = true; + c = favorite[c]; + len++; + } + if (len == 2) { + total += (2 + chain[i] + chain[favorite[i]]); + } else { + maxCycle = max(maxCycle, len); + } + } + } + return max(total, maxCycle); + + } +};