Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

}
};
Loading