File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed
Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ function solution ( k , dungeons ) {
2+ let maxDepth = 0 ;
3+
4+ function DFS ( 현재피로도 , currentDepth , visited ) {
5+ maxDepth = Math . max ( maxDepth , currentDepth ) ;
6+
7+ for ( let i = 0 ; i < dungeons . length ; i ++ ) {
8+ if ( visited [ i ] ) continue ;
9+
10+ const [ 최소필요피로도 , 소모피로도 ] = dungeons [ i ] ;
11+
12+ if ( 현재피로도 >= 최소필요피로도 ) {
13+ visited [ i ] = true ;
14+ DFS ( 현재피로도 - 소모피로도 , currentDepth + 1 , visited ) ;
15+ visited [ i ] = false ;
16+ }
17+ }
18+ }
19+
20+ const visited = Array ( dungeons . length ) . fill ( false ) ;
21+ DFS ( k , 0 , visited ) ;
22+
23+ return maxDepth ;
24+ }
25+
26+ console . log (
27+ solution ( 80 , [
28+ [ 80 , 20 ] ,
29+ [ 50 , 40 ] ,
30+ [ 30 , 10 ] ,
31+ ] )
32+ ) ;
Original file line number Diff line number Diff line change 1+ function solution ( n , wires ) {
2+ const graph = Array . from ( { length : n + 1 } , ( ) => [ ] ) ;
3+
4+ for ( let [ w1 , w2 ] of wires ) {
5+ graph [ w1 ] . push ( w2 ) ;
6+ graph [ w2 ] . push ( w1 ) ;
7+ }
8+
9+ let min = Infinity ;
10+
11+ for ( let [ cutA , cutB ] of wires ) {
12+ const visited = Array ( n + 1 ) . fill ( false ) ;
13+
14+ function DFS ( node ) {
15+ visited [ node ] = true ;
16+ let count = 1 ;
17+
18+ for ( const next of graph [ node ] ) {
19+ if ( ! visited [ next ] && ! ( next === cutB ) ) {
20+ count += DFS ( next ) ;
21+ }
22+ }
23+
24+ return count ;
25+ }
26+
27+ const countA = DFS ( cutA ) ;
28+ const countB = n - countA ;
29+
30+ min = Math . min ( min , Math . abs ( countA - countB ) ) ;
31+ }
32+
33+ return min ;
34+ }
35+
36+ console . log (
37+ solution ( 9 , [
38+ [ 1 , 3 ] ,
39+ [ 2 , 3 ] ,
40+ [ 3 , 4 ] ,
41+ [ 4 , 5 ] ,
42+ [ 4 , 6 ] ,
43+ [ 4 , 7 ] ,
44+ [ 7 , 8 ] ,
45+ [ 7 , 9 ] ,
46+ ] )
47+ ) ;
You can’t perform that action at this time.
0 commit comments