File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const fs = require ( "fs" ) ;
2+ const path = require ( "path" ) ;
3+
4+ const filePath =
5+ process . platform === "linux"
6+ ? "/dev/stdin"
7+ : path . join ( __dirname , "input.txt" ) ;
8+
9+ const input = fs
10+ . readFileSync ( filePath , "utf8" )
11+ . toString ( )
12+ . trim ( )
13+ . split ( "\n" )
14+ . map ( Number ) ;
15+
16+ function solution ( input ) {
17+ const T = input [ 0 ] ;
18+ const arr = input . slice ( 1 ) ;
19+ const result = [ ] ;
20+
21+ function dfs ( sum , target ) {
22+ if ( sum === target ) return 1 ;
23+ if ( sum > target ) return 0 ;
24+
25+ return dfs ( sum + 1 , target ) + dfs ( sum + 2 , target ) + dfs ( sum + 3 , target ) ;
26+ }
27+
28+ for ( let i = 0 ; i < T ; i ++ ) {
29+ const n = arr [ i ] ;
30+ result . push ( dfs ( 0 , n ) ) ;
31+ }
32+
33+ return result ;
34+ }
35+
36+ console . log ( solution ( input ) . join ( "\n" ) ) ;
Original file line number Diff line number Diff line change 1+ const fs = require ( "fs" ) ;
2+ const path = require ( "path" ) ;
3+
4+ const filePath =
5+ process . platform === "linux"
6+ ? "/dev/stdin"
7+ : path . join ( __dirname , "input.txt" ) ;
8+
9+ const input = fs
10+ . readFileSync ( filePath , "utf8" )
11+ . toString ( )
12+ . trim ( )
13+ . split ( "\n" )
14+ . map ( ( value ) => value . split ( " " ) . map ( Number ) ) ;
15+
16+ function solution ( input ) {
17+ const N = input [ 0 ] [ 0 ] ;
18+ const costs = input . slice ( 1 ) ;
19+ const dp = [ ...costs [ 0 ] ] ;
20+
21+ for ( let i = 1 ; i < N ; i ++ ) {
22+ const temp = [ 0 , 0 , 0 ] ;
23+ for ( let j = 0 ; j < 3 ; j ++ ) {
24+ if ( j === 0 ) temp [ 0 ] = costs [ i ] [ 0 ] + Math . min ( dp [ 1 ] , dp [ 2 ] ) ;
25+ else if ( j === 1 ) temp [ 1 ] = costs [ i ] [ 1 ] + Math . min ( dp [ 0 ] , dp [ 2 ] ) ;
26+ else if ( j === 2 ) temp [ 2 ] = costs [ i ] [ 2 ] + Math . min ( dp [ 0 ] , dp [ 1 ] ) ;
27+ }
28+ dp . splice ( 0 , 3 , ...temp ) ;
29+ }
30+
31+ return Math . min ( ...dp ) ;
32+ }
33+
34+ console . log ( solution ( input ) ) ;
Original file line number Diff line number Diff line change 1+ function solution ( n , edge ) {
2+ const map = Array ( n + 1 )
3+ . fill ( )
4+ . map ( ( ) => [ ] ) ;
5+ for ( let i = 0 ; i < edge . length ; i ++ ) {
6+ let a = edge [ i ] [ 0 ] ;
7+ let b = edge [ i ] [ 1 ] ;
8+ map [ a ] . push ( b ) ;
9+ map [ b ] . push ( a ) ;
10+ }
11+
12+ const visited = Array ( n + 1 ) . fill ( 0 ) ;
13+ const q = [ ] ;
14+ q . push ( 1 ) ;
15+ visited [ 1 ] = 1 ;
16+
17+ while ( q . length > 0 ) {
18+ const now = q . shift ( ) ;
19+ for ( let i = 0 ; i < map [ now ] . length ; i ++ ) {
20+ let next = map [ now ] [ i ] ;
21+ if ( visited [ next ] === 0 ) {
22+ visited [ next ] = visited [ now ] + 1 ;
23+ q . push ( next ) ;
24+ }
25+ }
26+ }
27+
28+ let max = 0 ;
29+ for ( let i = 0 ; i < visited . length ; i ++ ) {
30+ if ( visited [ i ] > max ) {
31+ max = visited [ i ] ;
32+ }
33+ }
34+
35+ let answer = 0 ;
36+ for ( let i = 0 ; i < visited . length ; i ++ ) {
37+ if ( visited [ i ] === max ) {
38+ answer ++ ;
39+ }
40+ }
41+
42+ return answer ;
43+ }
44+
45+ console . log (
46+ solution ( [
47+ [ 3 , 6 ] ,
48+ [ 4 , 3 ] ,
49+ [ 3 , 2 ] ,
50+ [ 1 , 3 ] ,
51+ [ 1 , 2 ] ,
52+ [ 2 , 4 ] ,
53+ [ 5 , 2 ] ,
54+ ] )
55+ ) ;
You can’t perform that action at this time.
0 commit comments