From 74f108b9c135d1e95d16b48876549336d2fc1274 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 26 Jan 2025 17:26:40 +0530 Subject: [PATCH] Create 2127. Maximum Employees to Be Invited to a Meeting --- ...ximum Employees to Be Invited to a Meeting | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2127. Maximum Employees to Be Invited to a Meeting 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); + + } +};