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 filePath =
3+ process . platform === "linux" ? "/dev/stdin" : "./서정우/input.txt" ;
4+ const input = fs
5+ . readFileSync ( filePath )
6+ . toString ( )
7+ . trim ( )
8+ . split ( "\n" )
9+ . map ( ( el ) => el . trim ( ) ) ;
10+
11+ const houseCount = Number ( input [ 0 ] ) ;
12+ const houses = input . slice ( 1 ) . map ( ( line ) => line . split ( " " ) . map ( Number ) ) ;
13+
14+ const dp = Array . from ( { length : houseCount } , ( ) => new Array ( 3 ) . fill ( 0 ) ) ;
15+
16+ for ( let i = 0 ; i < houseCount ; i ++ ) {
17+ const [ r , g , b ] = houses [ i ] ;
18+ if ( i === 0 ) {
19+ dp [ i ] [ 0 ] = r ;
20+ dp [ i ] [ 1 ] = g ;
21+ dp [ i ] [ 2 ] = b ;
22+ } else {
23+ dp [ i ] [ 0 ] = Math . min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + r ;
24+ dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + g ;
25+ dp [ i ] [ 2 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + b ;
26+ }
27+ }
28+
29+ console . log ( Math . min ( ...dp [ houseCount - 1 ] ) ) ;
Original file line number Diff line number Diff line change 1- 2
2- ABC-0123
3- AAA-9999
1+ 3
2+ 26 40 83
3+ 49 60 57
4+ 13 89 99
You can’t perform that action at this time.
0 commit comments