-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (132 loc) · 3.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const fs = require('fs');
const readline = require('readline');
const instream = fs.createReadStream('input.txt');
const outstream = fs.createWriteStream('output.txt');
const rl = readline.createInterface({
input: instream
});
rl.once('line', line => {
// function generate(n, s = '', op_br = 0, cl_br = 0) {
// if ((op_br + cl_br) === 2 * n) {
// outstream.write(s.toString());
// outstream.write('\n');
// }
// if (op_br < n) {
// generate(n, s + '(', op_br + 1, cl_br);
// }
// if (op_br > cl_br) {
// generate(n, s + ')', op_br, cl_br + 1);
// }
// }
function generate(s) {
let n = s.split('').length;
let ans = "No solution";
let depth = 0;
for (let i = n - 1; i >= 0; --i) {
if (s[i] === '(')
--depth;
else
++depth;
if (s[i] === '(' && depth > 0) {
--depth;
let open = (n - i - 1 - depth) / 2;
let close = n - i - 1 - open;
let openStr = '';
let closeStr = '';
for (let i = 0; i < open; i++) {
openStr += '(';
}
for (let i = 0; i < close; i++) {
closeStr += ')';
}
ans = s.slice(0, i) + ')' + openStr + closeStr ;
break;
}
}
return ans;
}
let arr = '';
let nn = 0;
while(nn < line * 2) {
if (nn < line) {
arr += '(';
nn++;
} else {
arr += ')';
nn++;
}
}
while(generate(arr) !== 'No solution') {
console.log(generate(arr))
}
});
rl.on('close', function() {
outstream.end();
});
// var stream = fs.createReadStream('file.txt', options);
// var byteSize = 10;
//
// stream.on("readable", function() {
// var chunk;
// while ( (chunk = stream.read(byteSize)) ) {
// console.log(chunk.length);
// }
// });
// const lines = [5, 1, 1, 2 , 3, 3];
// rl.on('line', line => lines.push(line));
//
// rl.on('close', () => {
// const [qt, ...restLines] = lines;
// const unique = [];
//
// for(let i = 0; i < qt; i++) {
// if (restLines[i] !== restLines[i - 1]) {
// unique.push(restLines[i])
// }
// }
//
// process.stdout.write(unique.toString());
// });
// return maxCounter;
// lines.reduce((acc, cur) => {
// if (cur === '1') {
// acc++;
// if (maxCounter < acc) {
// maxCounter = acc;
// }
// return acc;
// } else {
// acc = 0;
// return acc
// }
// }, 0);
// rl.on('line', (line) => {
// lines.push(line);
// });
// rl.on('close', () => {
// const [gold, stone] = lines;
//
// let result = 0;
//
// for (let item of stone) {
// if (gold.includes(item)) {
// result++;
// }
// }
// process.stdout.write(result.toString());
// });
// process.env.UV_THREADPOOL_SIZE = 1;
// const express = require('express');
// const app = express();
//
// app.get('/', (req, res) => {
// setTimeout(() => {
// res.send('Hola....')
// }, 5000);
// });
//
// app.get('/fast', (req, res) => {
// res.send('Fast endpoint');
// });
//
// app.listen(3000);