Skip to content

Commit 0a59bd7

Browse files
Merge pull request #690 from jinoo0306/main
[조진우] 105차 라이브 코테 제출
2 parents 78e7076 + cec3d58 commit 0a59bd7

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
.toString()
9+
.trim()
10+
.split("\n")
11+
.map((line) => line.split(" ").map(Number));
12+
13+
function solution(input) {
14+
const [N, M, K, X] = input[0];
15+
const graph = Array.from({ length: N + 1 }, () => []);
16+
const dist = Array(N + 1).fill(-1);
17+
18+
for (let i = 1; i <= M; i++) {
19+
const [a, b] = input[i];
20+
graph[a].push(b);
21+
}
22+
23+
const queue = [X];
24+
dist[X] = 0;
25+
26+
while (queue.length > 0) {
27+
const current = queue.shift();
28+
for (const next of graph[current]) {
29+
if (dist[next] === -1) {
30+
dist[next] = dist[current] + 1;
31+
queue.push(next);
32+
}
33+
}
34+
}
35+
36+
const answer = [];
37+
for (let i = 1; i <= N; i++) {
38+
if (dist[i] === K) answer.push(i);
39+
}
40+
41+
if (answer.length === 0) {
42+
return -1;
43+
} else {
44+
return answer.sort((a, b) => a - b).join("\n");
45+
}
46+
}
47+
48+
console.log(solution(input));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
.toString()
9+
.trim()
10+
.split("\n");
11+
12+
function solution(input) {
13+
const n = +input[0];
14+
const check = Array.from({ length: 26 }, () => Array(26).fill(false));
15+
16+
for (let i = 1; i <= n; i++) {
17+
const [a, , b] = input[i].split(" ");
18+
const x = a.charCodeAt(0) - 97;
19+
const y = b.charCodeAt(0) - 97;
20+
check[y][x] = true; // ✅ 방향 반대로!
21+
}
22+
23+
for (let k = 0; k < 26; k++) {
24+
for (let i = 0; i < 26; i++) {
25+
for (let j = 0; j < 26; j++) {
26+
if (check[i][k] && check[k][j]) {
27+
check[i][j] = true;
28+
}
29+
}
30+
}
31+
}
32+
33+
const m = +input[n + 1];
34+
const result = [];
35+
36+
for (let i = n + 2; i < n + 2 + m; i++) {
37+
const [a, , b] = input[i].split(" ");
38+
const x = a.charCodeAt(0) - 97;
39+
const y = b.charCodeAt(0) - 97;
40+
41+
if (x === y) {
42+
result.push("F");
43+
} else {
44+
result.push(check[y][x] ? "T" : "F");
45+
}
46+
}
47+
48+
return result.join("\n");
49+
}
50+
51+
console.log(solution(input));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function solution(order) {
2+
let box = 1;
3+
let count = 0;
4+
let tempBox = [];
5+
let i = 0;
6+
7+
while (box <= order.length) {
8+
if (order[i] === box) {
9+
count++;
10+
i++;
11+
box++;
12+
} else if (tempBox.length > 0 && tempBox[tempBox.length - 1] === order[i]) {
13+
tempBox.pop();
14+
count++;
15+
i++;
16+
} else {
17+
tempBox.push(box);
18+
box++;
19+
}
20+
}
21+
22+
while (tempBox.length > 0) {
23+
if (tempBox[tempBox.length - 1] === order[i]) {
24+
tempBox.pop();
25+
count++;
26+
i++;
27+
} else {
28+
break;
29+
}
30+
}
31+
32+
return count;
33+
}
34+
35+
console.log(solution([4, 3, 1, 2, 5]));

0 commit comments

Comments
 (0)