File tree 1 file changed +51
-0
lines changed 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } favorite
3
+ * @return {number }
4
+ */
5
+ const maximumInvitations = function ( favorite ) {
6
+ const n = favorite . length
7
+ const indegree = Array ( n ) . fill ( 0 )
8
+ for ( let i = 0 ; i < n ; i ++ ) indegree [ favorite [ i ] ] ++
9
+ const { max } = Math
10
+ let q = [ ]
11
+ const visited = Array ( n ) . fill ( 0 )
12
+ const depth = Array ( n ) . fill ( 1 )
13
+ for ( let i = 0 ; i < n ; i ++ ) {
14
+ if ( indegree [ i ] === 0 ) {
15
+ depth [ i ] = 1
16
+ visited [ i ] = 1
17
+ q . push ( i )
18
+ }
19
+ }
20
+
21
+ while ( q . length ) {
22
+ const cur = q . shift ( )
23
+ const nxt = favorite [ cur ]
24
+ indegree [ nxt ] --
25
+ if ( indegree [ nxt ] == 0 ) {
26
+ q . push ( nxt )
27
+ visited [ nxt ] = 1
28
+ }
29
+ depth [ nxt ] = depth [ cur ] + 1
30
+ }
31
+
32
+ let max_circle_size = 0
33
+ let max_link_size = 0
34
+ for ( let i = 0 ; i < n ; i ++ ) {
35
+ if ( visited [ i ] === 1 ) continue
36
+ let j = i
37
+ let count = 0
38
+ while ( visited [ j ] == 0 ) {
39
+ count ++
40
+ visited [ j ] = 1
41
+ j = favorite [ j ]
42
+ }
43
+ if ( count > 2 ) max_circle_size = max ( max_circle_size , count )
44
+ else if ( count == 2 ) max_link_size += depth [ i ] + depth [ favorite [ i ] ]
45
+ }
46
+
47
+ return max ( max_circle_size , max_link_size )
48
+ }
49
+
50
+ // another
51
+
1
52
/**
2
53
* @param {number[] } favorite
3
54
* @return {number }
You can’t perform that action at this time.
0 commit comments