Skip to content

Commit

Permalink
Create 2127. Maximum Employees to Be Invited to a Meeting (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Jan 26, 2025
2 parents 37e1217 + 74f108b commit 62c69ab
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions 2127. Maximum Employees to Be Invited to a Meeting
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution {
public:
int maximumInvitations(vector<int>& favorite) {

int n = favorite.size();
vector<int> indegree(n, 0);
vector<int> chain(n, 0);
vector<bool> vis(n, false);
for (int i : favorite) {
indegree[i]++;
}
queue<int> 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);

}
};

0 comments on commit 62c69ab

Please sign in to comment.