Skip to content

Commit

Permalink
Time: 18 ms (95.39%), Space: 60.8 MB (66.45%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Jan 26, 2025
1 parent aea4971 commit cd38e9f
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public int maximumInvitations(int[] favorite) {
int n = favorite.length;
int inDeg[] = new int[n];
int chainLen[] = new int[n];
boolean visited[] = new boolean[n];
Queue<Integer> q = new LinkedList<>();

for (int f : favorite) inDeg[f]++;

for (int i = 0; i < n; i++) if (inDeg[i] == 0) q.add(i);

while (!q.isEmpty()) {
int u = q.poll();
visited[u] = true;
int v = favorite[u];
chainLen[v] = Math.max(chainLen[v], chainLen[u] + 1);
if (--inDeg[v] == 0) q.add(v);
}

int maxCycle = 0, pairChains = 0;

for (int i = 0; i < n; i++) {
if (visited[i]) continue;

int cycleLen = 0, current = i;
while (!visited[current]) {
visited[current] = true;
current = favorite[current];
cycleLen++;
}

if (cycleLen == 2) pairChains += 2 + chainLen[i] + chainLen[favorite[i]];
else maxCycle = Math.max(maxCycle, cycleLen);
}

return Math.max(maxCycle, pairChains);
}
}

0 comments on commit cd38e9f

Please sign in to comment.