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 (
3+ process . platform === "linux"
4+ ? "/dev/stdin"
5+ : require ( "path" ) . join ( __dirname , "input.txt" ) ,
6+ "utf8"
7+ )
8+ . trim ( ) ;
9+
10+ const LEFT_PAREN = "(" ;
11+ const RIGHT_PAREN = ")" ;
12+
13+ function solution ( expr ) {
14+ const stack = [ ] ;
15+ const pairs = [ ] ;
16+
17+ for ( let i = 0 ; i < expr . length ; i ++ ) {
18+ if ( expr [ i ] === LEFT_PAREN ) {
19+ stack . push ( i ) ;
20+ } else if ( expr [ i ] === RIGHT_PAREN ) {
21+ const openIdx = stack . pop ( ) ;
22+ pairs . push ( [ openIdx , i ] ) ;
23+ }
24+ }
25+
26+ const results = new Set ( ) ;
27+ const total = pairs . length ;
28+
29+ const dfs = ( depth , selected ) => {
30+ if ( depth === total ) {
31+ if ( selected . length === 0 ) return ;
32+
33+ const toRemove = new Set ( selected . flat ( ) ) ;
34+ let str = "" ;
35+ for ( let i = 0 ; i < expr . length ; i ++ ) {
36+ if ( ! toRemove . has ( i ) ) {
37+ str += expr [ i ] ;
38+ }
39+ }
40+ results . add ( str ) ;
41+ return ;
42+ }
43+
44+ dfs ( depth + 1 , [ ...selected , pairs [ depth ] ] ) ;
45+ dfs ( depth + 1 , selected ) ;
46+ } ;
47+
48+ dfs ( 0 , [ ] ) ;
49+
50+ const sorted = [ ...results ] . sort ( ) ;
51+ return sorted . join ( "\n" ) ;
52+ }
53+
54+ console . log ( solution ( input ) ) ;
Original file line number Diff line number Diff line change 1+ const input = require ( "fs" )
2+ . readFileSync (
3+ process . platform === "linux"
4+ ? "/dev/stdin"
5+ : require ( "path" ) . join ( __dirname , "input.txt" ) ,
6+ "utf8"
7+ )
8+ . trim ( )
9+ . split ( "\n" ) ;
10+
11+ function solution ( input ) {
12+ const N = Number ( input [ 0 ] ) ;
13+ const circles = [ ] ;
14+
15+ for ( let i = 1 ; i <= N ; i ++ ) {
16+ const [ x , r ] = input [ i ] . split ( " " ) . map ( Number ) ;
17+ const id = i ;
18+ circles . push ( [ x - r , id , 0 ] ) ;
19+ circles . push ( [ x + r , id , 1 ] ) ;
20+ }
21+
22+ circles . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
23+
24+ const stack = [ ] ;
25+
26+ for ( const [ _ , id , type ] of circles ) {
27+ if ( type === 0 ) {
28+ stack . push ( [ id , type ] ) ;
29+ } else {
30+ if ( stack . length === 0 || stack [ stack . length - 1 ] [ 0 ] !== id ) {
31+ return "NO" ;
32+ }
33+ stack . pop ( ) ;
34+ }
35+ }
36+
37+ return "YES" ;
38+ }
39+
40+ console . log ( solution ( input ) ) ;
You can’t perform that action at this time.
0 commit comments