File tree Expand file tree Collapse file tree 4 files changed +87
-0
lines changed
search-in-rotated-sorted-array Expand file tree Collapse file tree 4 files changed +87
-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+ } ;
Original file line number Diff line number Diff line change 1+ var invertTree = function ( root ) {
2+ if ( root === null ) return null ;
3+
4+ let queue = [ root ] ;
5+
6+ while ( queue . length > 0 ) {
7+ let node = queue . shift ( ) ;
8+
9+ // ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ์์์ ๊ตํ
10+ [ node . left , node . right ] = [ node . right , node . left ] ;
11+
12+ if ( node . left ) queue . push ( node . left ) ;
13+ if ( node . right ) queue . push ( node . right ) ;
14+ }
15+
16+ return root ;
17+ } ;
Original file line number Diff line number Diff line change 1+ var canJump = function ( nums ) {
2+ let maxReach = 0 ;
3+
4+ for ( let i = 0 ; i < nums . length ; i ++ ) {
5+ if ( i > maxReach ) return false ;
6+ maxReach = Math . max ( maxReach , i + nums [ i ] ) ;
7+ }
8+
9+ return true ;
10+ } ;
Original file line number Diff line number Diff line change 1+ var search = function ( nums , target ) {
2+ let left = 0 ,
3+ right = nums . length - 1 ;
4+
5+ while ( left <= right ) {
6+ const mid = Math . floor ( ( left + right ) / 2 ) ;
7+
8+ if ( nums [ mid ] === target ) return mid ;
9+
10+ // ์ผ์ชฝ ์ ๋ฐ์ด ์ ๋ ฌ๋์ด ์๋ ๊ฒฝ์ฐ
11+ if ( nums [ left ] <= nums [ mid ] ) {
12+ if ( nums [ left ] <= target && target < nums [ mid ] ) {
13+ right = mid - 1 ; // ์ผ์ชฝ ๋ฒ์๋ก ์ด๋
14+ } else {
15+ left = mid + 1 ; // ์ค๋ฅธ์ชฝ ๋ฒ์๋ก ์ด๋
16+ }
17+ }
18+ // ์ค๋ฅธ์ชฝ ์ ๋ฐ์ด ์ ๋ ฌ๋์ด ์๋ ๊ฒฝ์ฐ
19+ else {
20+ if ( nums [ mid ] < target && target <= nums [ right ] ) {
21+ left = mid + 1 ; // ์ค๋ฅธ์ชฝ ๋ฒ์๋ก ์ด๋
22+ } else {
23+ right = mid - 1 ; // ์ผ์ชฝ ๋ฒ์๋ก ์ด๋
24+ }
25+ }
26+ }
27+
28+ return - 1 ;
29+ } ;
You canโt perform that action at this time.
0 commit comments