File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const fs = require ( 'fs' ) ;
4
+
5
+ process . stdin . resume ( ) ;
6
+ process . stdin . setEncoding ( 'utf-8' ) ;
7
+
8
+ let inputString = '' ;
9
+ let currentLine = 0 ;
10
+
11
+ process . stdin . on ( 'data' , inputStdin => {
12
+ inputString += inputStdin ;
13
+ } ) ;
14
+
15
+ process . stdin . on ( 'end' , _ => {
16
+ inputString = inputString . replace ( / \s * $ / , '' )
17
+ . split ( '\n' )
18
+ . map ( str => str . replace ( / \s * $ / , '' ) ) ;
19
+
20
+ main ( ) ;
21
+ } ) ;
22
+
23
+ function readLine ( ) {
24
+ return inputString [ currentLine ++ ] ;
25
+ }
26
+
27
+ // Complete the toys function below.
28
+ function toys ( w ) {
29
+ w = w . sort ( ( x , y ) => x - y ) ;
30
+ let count = 1 ;
31
+ let check = w . shift ( ) + 4 ;
32
+ w . forEach ( x => {
33
+ if ( x > check ) {
34
+ count ++ ;
35
+ check = x + 4 ;
36
+ }
37
+ } ) ;
38
+ return count ;
39
+ }
40
+
41
+ function main ( ) {
42
+ const ws = fs . createWriteStream ( process . env . OUTPUT_PATH ) ;
43
+
44
+ const n = parseInt ( readLine ( ) , 10 ) ;
45
+
46
+ const w = readLine ( ) . split ( ' ' ) . map ( wTemp => parseInt ( wTemp , 10 ) ) ;
47
+
48
+ let result = toys ( w ) ;
49
+
50
+ ws . write ( result + "\n" ) ;
51
+
52
+ ws . end ( ) ;
53
+ }
You can’t perform that action at this time.
0 commit comments