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 input = require ( 'fs' )
2+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
3+ . toString ( )
4+ . trim ( )
5+ . split ( '\n' )
6+ . map ( ( el ) => el . split ( ' ' ) . map ( Number ) ) ;
7+
8+ function solution ( input ) {
9+ const [ N , M ] = input [ 0 ] ;
10+ const treeH = input [ 1 ] . sort ( ( a , b ) => a - b ) ;
11+
12+ let start = 0 ;
13+ let end = treeH [ treeH . length - 1 ] ;
14+ let answer = 0 ;
15+
16+ while ( start <= end ) {
17+ let mid = Math . floor ( ( start + end ) / 2 ) ;
18+ let sum = 0 ;
19+
20+ for ( let x of treeH ) {
21+ if ( x > mid ) sum += x - mid ;
22+ }
23+
24+ if ( sum >= M ) {
25+ answer = mid ;
26+ start = mid + 1 ;
27+ } else {
28+ end = mid - 1 ;
29+ }
30+ }
31+
32+ return answer ;
33+ }
34+
35+ console . log ( solution ( input ) ) ;
Original file line number Diff line number Diff line change 1+ const input = require ( 'fs' )
2+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
3+ . toString ( )
4+ . trim ( )
5+ . split ( '\n' )
6+ . map ( ( el ) => el . split ( ' ' ) . map ( Number ) ) ;
7+
8+ function solution ( input ) {
9+ const N = input [ 0 ] [ 0 ] ;
10+ const 지방예산요청 = input [ 1 ] . sort ( ( a , b ) => a - b ) ;
11+ const M = input [ 2 ] [ 0 ] ;
12+
13+ let high = 지방예산요청 [ N - 1 ] ;
14+ let low = 0 ;
15+
16+ while ( low <= high ) {
17+ const mid = Math . floor ( ( low + high ) / 2 ) ;
18+ const sum = 지방예산요청 . reduce ( ( acc , v ) => acc + ( v <= mid ? v : mid ) , 0 ) ;
19+ if ( sum > M ) {
20+ high = mid - 1 ;
21+ } else {
22+ low = mid + 1 ;
23+ }
24+ }
25+ return high ;
26+ }
27+
28+ console . log ( solution ( input ) ) ;
Original file line number Diff line number Diff line change 1+ function solution ( files ) {
2+ let answerWrap = files . map ( ( file , index ) => ( { file, index } ) ) ;
3+
4+ const compare = ( a , b ) => {
5+ const reg = / ( \D * ) ( [ 0 - 9 ] * ) / i;
6+ const A = a . match ( reg ) ;
7+ const B = b . match ( reg ) ;
8+
9+ const compareHead = A [ 1 ] . toLowerCase ( ) . localeCompare ( B [ 1 ] . toLowerCase ( ) ) ;
10+ const compareNumber = ( a , b ) => {
11+ return parseInt ( a ) > parseInt ( b ) ? 1 : parseInt ( a ) < parseInt ( b ) ? - 1 : 0 ;
12+ } ;
13+ return compareHead === 0 ? compareNumber ( A [ 2 ] , B [ 2 ] ) : compareHead ;
14+ } ;
15+
16+ answerWrap . sort ( ( a , b ) => {
17+ const result = compare ( a . file , b . file ) ;
18+ return result === 0 ? a . index - b . index : result ;
19+ } ) ;
20+
21+ return answerWrap . map ( ( answer ) => answer . file ) ;
22+ }
23+
24+ console . log (
25+ solution ( [
26+ 'img12.png' ,
27+ 'img10.png' ,
28+ 'img02.png' ,
29+ 'img1.png' ,
30+ 'IMG01.GIF' ,
31+ 'img2.JPG' ,
32+ ] )
33+ ) ;
You can’t perform that action at this time.
0 commit comments