Skip to content

Commit d7472cf

Browse files
authored
Merge pull request #592 from eric-hjh/main
[황장현] 83차 라이브 코테 제출
2 parents 270fbc1 + 66857cf commit d7472cf

File tree

3 files changed

+122
-8
lines changed

3 files changed

+122
-8
lines changed

live8/test82/문제2/황장현.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,53 @@ const input = fs
1010
function solution(input) {
1111
const [N, M] = input[0];
1212
const relation = input.slice(1);
13-
const graph = Array.from({ length: N + 1 }).map((_) =>
14-
Array.from({ length: 0 })
15-
);
13+
const graph = Array.from({ length: N + 1 }, () => []);
1614

1715
for (const [f1, f2] of relation) {
1816
graph[f1].push(f2);
1917
graph[f2].push(f1);
2018
}
2119

22-
function dfs(start, target) {}
20+
function bfs(start, target) {
21+
const queue = [[start, 0]];
22+
const visited = Array(N + 1).fill(false);
23+
visited[start] = true;
24+
25+
while (queue.length > 0) {
26+
const [current, depth] = queue.shift();
27+
28+
if (current === target) return depth;
29+
30+
for (let nearNode of graph[current]) {
31+
if (!visited[nearNode]) {
32+
visited[nearNode] = true;
33+
queue.push([nearNode, depth + 1]);
34+
}
35+
}
36+
}
37+
return Infinity;
38+
}
39+
40+
let minKevinBacon = Infinity;
41+
let minUser = -1;
2342

2443
for (let i = 1; i <= N; i++) {
25-
const temp = [];
44+
let kevinBacon = 0;
2645
for (let j = 1; j <= N; j++) {
27-
if (graph[i].includes(j)) temp.push(1);
28-
else dfs(i, j);
46+
if (i !== j) {
47+
kevinBacon += bfs(i, j);
48+
}
49+
}
50+
51+
if (kevinBacon < minKevinBacon) {
52+
minKevinBacon = kevinBacon;
53+
minUser = i;
54+
} else if (kevinBacon === minKevinBacon) {
55+
minUser = Math.min(minUser, i);
2956
}
3057
}
58+
59+
console.log(minUser);
3160
}
3261

33-
console.log(solution(input));
62+
solution(input);

live8/test83/문제1/황장현.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 binarySearch(arr, target) {
9+
let start = 0;
10+
let end = arr.length - 1;
11+
12+
while (start <= end) {
13+
let mid = Math.floor((start + end) / 2);
14+
15+
if (arr[mid] === target) return 1;
16+
if (arr[mid] < target) start = mid + 1;
17+
else end = mid - 1;
18+
}
19+
20+
return 0;
21+
}
22+
23+
function solution(input) {
24+
const N = input[0][0];
25+
const cards = input[1];
26+
const M = input[2][0];
27+
const sangenCards = input[3];
28+
29+
cards.sort((a, b) => a - b);
30+
31+
const result = sangenCards.map((sangen) => binarySearch(cards, sangen));
32+
33+
return result.join(' ');
34+
}
35+
36+
console.log(solution(input));

live8/test83/문제2/황장현.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
const LIMIT = 1299709;
9+
const isPrime = Array(LIMIT + 1).fill(true);
10+
isPrime[0] = isPrime[1] = false;
11+
const primes = [];
12+
13+
for (let i = 2; i <= LIMIT; i++) {
14+
if (isPrime[i]) {
15+
primes.push(i);
16+
for (let j = i * 2; j <= LIMIT; j += i) {
17+
isPrime[j] = false;
18+
}
19+
}
20+
}
21+
22+
function solution(input) {
23+
const T = input[0];
24+
const list = input.slice(1);
25+
const result = [];
26+
27+
for (let numb of list) {
28+
if (isPrime[numb]) {
29+
result.push(0);
30+
continue;
31+
}
32+
33+
let left = 0,
34+
right = primes.length - 1;
35+
while (left < right) {
36+
let mid = Math.floor((left + right) / 2);
37+
if (primes[mid] >= numb) right = mid;
38+
else left = mid + 1;
39+
}
40+
41+
let prevPrime = primes[left - 1];
42+
let nextPrime = primes[left];
43+
44+
result.push(nextPrime - prevPrime);
45+
}
46+
return result.join('\n');
47+
}
48+
49+
console.log(solution(input));

0 commit comments

Comments
 (0)