2
* @param {number[]} favorite5
var maximumInvitations = function (favorite) {7
let len = favorite.length;8
// count the number indegree9
let indegree = new Array(len).fill(0);10
// save the number relation12
for (let i = 0; i < favorite.length; i++) {13
indegree[favorite[i]]++;15
if (rMap.has(favorite[i])) {16
let arr = rMap.get(favorite[i]);18
rMap.set(favorite[i], arr);20
rMap.set(favorite[i], [i]);23
// find the 0 indegree number25
for (let i = 0; i < len; i++) {26
if (indegree[i] === 0) {30
// check if the circle exist32
// the length of uncircle34
// save the number and the length of uncircle36
while (arr.length > 0) {38
for (let i = 0; i < arr.length; i++) {40
let next = favorite[arr[i]];43
if (indegree[next] === 0) {47
// update the length of uncircle56
// find the not 0 indegree number57
let keys = find(indegree);58
// mark the search number in circle59
let circleMap = new Map();60
// the length of circle62
// the max length of circle64
// sum the length of circle68
while (cArr.length > 0) {70
for (let i = 0; i < cArr.length; i++) {71
// not find the circle72
if (!circleMap.has(cArr[i])) {73
circleMap.set(cArr[i], circleLen);74
tmp.push(favorite[cArr[i]]);77
maxCircleLen = Math.max(maxCircleLen, circleLen - 1);78
// if the length equals 2 then sum the length79
if (circleLen - 1 === 2) {81
calc(cArr[i], rMap, lMap) +82
calc(favorite[cArr[i]], rMap, lMap) +88
// find the next number not search89
for (let i = 0; i < keys.length; i++) {90
if (!circleMap.has(keys[i])) {100
res = Math.max(res, maxCircleLen, sumCircleLen);106
function calc(num, rMap, lMap) {108
let arr = rMap.has(num) ? rMap.get(num) : [];109
if (arr.length > 0) {110
for (let i = 0; i < arr.length; i++) {111
let l = lMap.has(arr[i]) ? lMap.get(arr[i]) : 0;112
res = Math.max(res, l);120
for (let i = 0; i < arr.length; i++) {