Skip to content

Commit 8f00be1

Browse files
committed
2 parents a13e35a + cccfb29 commit 8f00be1

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

Diff for: 4주차/박종운/골드4__사회망_서비스.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const readline = require('readline');
2+
3+
const rl = readline.createInterface({
4+
input: process.execArgv.includes("--stack-size=65536") ?
5+
process.stdin : require('fs').createReadStream(__dirname + '/input.txt'),
6+
output: process.stdout
7+
});
8+
9+
let N;
10+
let graph;
11+
let dp;
12+
let visited;
13+
14+
function dfs(cur) {
15+
visited[cur] = true;
16+
17+
for (const child of graph[cur]) {
18+
if (!visited[child]) {
19+
dfs(child);
20+
dp[cur][0] += dp[child][1];
21+
dp[cur][1] += Math.min(...dp[child]);
22+
}
23+
}
24+
}
25+
26+
rl.question('', input => {
27+
N = parseInt(input);
28+
graph = Array.from({ length: N + 1 }, () => []);
29+
dp = Array.from({ length: N + 1 }, () => [0, 1]);
30+
visited = Array(N + 1).fill(false);
31+
32+
rl.on('line', line => {
33+
const [u, v] = line.split(' ').map(Number);
34+
graph[u].push(v);
35+
graph[v].push(u);
36+
}).on('close', () => {
37+
dfs(1);
38+
console.log(Math.min(...dp[1]));
39+
40+
process.exit();
41+
});
42+
});

Diff for: 4주차/박종운/레벨3__길_찾기_게임.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function solution(nodeinfo) {
2+
const nodes = nodeinfo.map((el, i) => [i+1, ...el]);
3+
4+
const sorted = nodes.sort((a, b) => b[2] === a[2] ? a[1] - b[1] : b[2] - a[2]);
5+
6+
const preorder = [];
7+
const postorder = [];
8+
9+
function traverse(arr) {
10+
if(!arr.length) return;
11+
12+
const current = arr[0];
13+
14+
preorder.push(current[0]);
15+
16+
const newLeft = arr.filter(el => el[2] < current[2] && el[1] < current[1]);
17+
const newRight = arr.filter(el => el[2] < current[2] && el[1] > current[1] );
18+
19+
traverse(newLeft);
20+
traverse(newRight);
21+
22+
postorder.push(current[0]);
23+
}
24+
25+
traverse(sorted);
26+
27+
return [preorder, postorder];
28+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const local_input = `
2+
7
3+
1 6
4+
6 3
5+
3 5
6+
4 1
7+
2 4
8+
4 7
9+
`;
10+
11+
const input = process.execArgv.includes("--stack-size=65536")
12+
? require("fs").readFileSync("dev/stdin").toString()
13+
: local_input;
14+
15+
const lines = input.trim().split('\n');
16+
17+
const N = Number(lines[0]);
18+
const edges = lines.slice(1).map(el => el.split(' ').map(Number));
19+
20+
const graph = {};
21+
22+
edges.forEach(el => {
23+
if(!graph[el[0]]) graph[el[0]] = [];
24+
if(!graph[el[1]]) graph[el[1]] = [];
25+
graph[el[0]].push(el[1])
26+
graph[el[1]].push(el[0])
27+
})
28+
29+
const queue = [1];
30+
const visited = new Set([1]);
31+
const parentNodes = [];
32+
33+
while(queue.length) {
34+
const currentNode = queue.shift();
35+
const childNodes = graph[currentNode].filter(childNode => !visited.has(childNode));
36+
37+
childNodes.forEach(childNode => {
38+
parentNodes[childNode] = currentNode;
39+
queue.push(childNode);
40+
})
41+
42+
visited.add(currentNode);
43+
}
44+
45+
console.log(parentNodes.slice(2).join('\n'))

0 commit comments

Comments
 (0)