File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ var canFinish = function ( numCourses , prerequisites ) {
2
+ const graph = Array . from ( { length : numCourses } , ( ) => [ ] ) ;
3
+ const inDegree = Array ( numCourses ) . fill ( 0 ) ;
4
+
5
+ // ๊ทธ๋ํ ๋ง๋ค๊ธฐ ๋ฐ ์ง์
์ฐจ์ ๊ณ์ฐ
6
+ for ( const [ course , pre ] of prerequisites ) {
7
+ graph [ pre ] . push ( course ) ;
8
+ inDegree [ course ] ++ ;
9
+ }
10
+
11
+ // ์ง์
์ฐจ์๊ฐ 0์ธ ๋
ธ๋๋ถํฐ ์์
12
+ const queue = [ ] ;
13
+ for ( let i = 0 ; i < numCourses ; i ++ ) {
14
+ if ( inDegree [ i ] === 0 ) queue . push ( i ) ;
15
+ }
16
+
17
+ let count = 0 ;
18
+ while ( queue . length ) {
19
+ const node = queue . shift ( ) ;
20
+ count ++ ;
21
+
22
+ for ( const neighbor of graph [ node ] ) {
23
+ inDegree [ neighbor ] -- ;
24
+ if ( inDegree [ neighbor ] === 0 ) {
25
+ queue . push ( neighbor ) ;
26
+ }
27
+ }
28
+ }
29
+
30
+ return count === numCourses ;
31
+ } ;
You canโt perform that action at this time.
0 commit comments