Skip to content

Commit 78e7076

Browse files
Merge pull request #691 from eric-hjh/main
[황장현] 105차 라이브 코테 제출
2 parents 6eaea3f + 3939144 commit 78e7076

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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, K, X] = input[0];
10+
const graph = Array.from({ length: N + 1 }, () => []);
11+
const visited = Array.from({ length: N + 1 }, () => false);
12+
const info = input.slice(1);
13+
14+
for (let i = 0; i < M; i++) {
15+
const [a, b] = info[i];
16+
graph[a].push(b);
17+
}
18+
const queue = [];
19+
const result = [];
20+
queue.push([X, 0]);
21+
visited[X] = true;
22+
while (queue.length) {
23+
const [node, cnt] = queue.shift();
24+
if (cnt === K) {
25+
result.push(node);
26+
continue;
27+
}
28+
for (const next of graph[node]) {
29+
if (!visited[next]) {
30+
visited[next] = true;
31+
queue.push([next, cnt + 1]);
32+
}
33+
}
34+
}
35+
result.sort((a, b) => a - b);
36+
if (result.length === 0) {
37+
return -1;
38+
}
39+
return result.join('\n');
40+
}
41+
42+
console.log(solution(input));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function solution(order) {
2+
const stack = [];
3+
let result = 0;
4+
let current = 1;
5+
6+
for (let i = 0; i < order.length; i++) {
7+
while (
8+
current <= order.length &&
9+
(stack.length === 0 || stack[stack.length - 1] !== order[i])
10+
) {
11+
stack.push(current);
12+
current++;
13+
}
14+
if (stack[stack.length - 1] === order[i]) {
15+
stack.pop();
16+
result++;
17+
} else {
18+
break;
19+
}
20+
}
21+
22+
return result;
23+
}
24+
25+
console.log(solution([4, 3, 1, 2, 5]));

0 commit comments

Comments
 (0)