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 binarySearch ( arr , target ) {
9+ let start = 0 ;
10+ let end = arr . length - 1 ;
11+
12+ while ( start <= end ) {
13+ let mid = Math . floor ( ( start + end ) / 2 ) ;
14+
15+ if ( arr [ mid ] === target ) return 1 ;
16+ if ( arr [ mid ] < target ) start = mid + 1 ;
17+ else end = mid - 1 ;
18+ }
19+
20+ return 0 ;
21+ }
22+
23+ function solution ( input ) {
24+ const N = input [ 0 ] [ 0 ] ;
25+ const cards = input [ 1 ] ;
26+ const M = input [ 2 ] [ 0 ] ;
27+ const sangenCards = input [ 3 ] ;
28+
29+ cards . sort ( ( a , b ) => a - b ) ;
30+
31+ const result = sangenCards . map ( ( sangen ) => binarySearch ( cards , sangen ) ) ;
32+
33+ return result . join ( ' ' ) ;
34+ }
35+
36+ console . log ( solution ( input ) ) ;
You can’t perform that action at this time.
0 commit comments