Skip to content

Commit d3e4b60

Browse files
committed
백준 문제들 추가
1 parent b320947 commit d3e4b60

12 files changed

Lines changed: 326 additions & 112 deletions

JS/11.그래프/01.다익스트라 알고리즘.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ function solution(N, road, K) {
6060
graph[a].push([b, c]);
6161
}
6262

63-
minH.insert([1, 3]);
64-
minH.insert([1, 1]);
65-
minH.insert([1, 2]);
66-
minH.insert([1, 8888]);
67-
minH.insert([1, 88]);
68-
minH.insert([1, 8248]);
69-
console.log(minH);
7063
dist[1] = 0; // 시작점인 1번을 0으로 설정. 사용할 일이 없음.
7164
minH.insert([1, 0]); // 시작점을 minheap에 넣어준다.
7265

@@ -113,3 +106,4 @@ console.log(
113106
5
114107
)
115108
);
109+
//14
Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
function solution(n, edges, end) {
2-
let answer = 0;
3-
let graph = Array.from(Array(n + 1), () => Array());
4-
let dist = Array(from({ length: n + 1 }, () => 1000));
5-
let ch = Array(n + 1).fill(0);
6-
for (let [a, b, c] of edges) {
7-
graph[a].push([b, c]);
8-
}
2+
let graph = Array(n + 1);
3+
for (let i = 0; i < graph.length; i++) graph[i] = Array();
4+
let distances = Array(n + 1).fill(1e9);
5+
let checked = Array(n + 1).fill(0);
6+
7+
// forEach는 배열 요소 각각에 대해서 실행한다.
8+
edges.forEach(([start, end, cost]) => graph[start].push([end, cost]));
9+
10+
// 1부터 시작이므로 check
11+
distances[1] = 0;
912

10-
/** min Heap 쓰지 않는 다익스트라 **/
11-
dist[1] = 0;
13+
// n개의 노드만큼 실행해준다.
1214
for (let i = 1; i <= n; i++) {
1315
let min = 0;
16+
// 0번에 최대값을 이용해서 방문하지 않은 노드 번호를 min 값에 입력해준다.
17+
// 방문하지 않았고, dist의 값이 1e9보다 작으면 해당 노드에서 간선을 탐색한다.
1418
for (let j = 1; j <= n; j++) {
15-
if (ch[j] === 0 && dist[j] < dist[min]) min = j;
19+
if (checked[j] === 0 && distances[j] < distances[min]) min = j;
1620
}
17-
ch[min] = 1;
18-
for (let next of graph[min]) {
19-
if (dist[min] + cost < dist[next]) {
20-
dist[next] = dist[min] + cost;
21-
}
21+
// 연결된 간선에 도달하는 비용을 저장해준다.
22+
for (let [next, cost] of graph[min]) {
23+
if (distances[min] + cost < distances[next])
24+
distances[next] = distances[min] + cost;
2225
}
26+
// 처리한 노드는 방문 체크를 해준다.
27+
checked[min] = 1;
2328
}
24-
answer = dist[end];
25-
return answer;
29+
return distances[end];
2630
}
2731
console.log(
2832
solution(
2933
6,
30-
[1, 2, 3, 4, 5],
3134
[
32-
[1, 3, 6],
33-
[2, 2, 5],
34-
[1, 5, 2],
35+
[1, 2, 12],
36+
[1, 3, 4],
37+
[2, 1, 2],
3538
[2, 3, 5],
36-
]
39+
[2, 5, 5],
40+
[3, 4, 5],
41+
[4, 2, 2],
42+
[4, 5, 5],
43+
[6, 4, 5],
44+
],
45+
5
3746
)
38-
); // [17, 12]
47+
);
48+
// 14

JS/11.그래프/test.js

Lines changed: 36 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,48 @@
1-
class minHeap {
2-
constructor() {
3-
this.heap = [];
4-
this.heap.push([Number.MIN_SAFE_INTEGER, 0]);
5-
}
6-
insert([a, b]) {
7-
this.heap.push([a, b]);
8-
this.upheap(this.heap.length - 1);
9-
}
10-
upheap(pos) {
11-
let tmp = this.heap[pos];
12-
while (tmp[1] < this.heap[parseInt(pos / 2)][1]) {
13-
this.heap[pos] = this.heap[parseInt(pos / 2)];
14-
pos = parseInt(pos / 2);
15-
}
16-
this.heap[pos] = tmp;
17-
}
18-
get() {
19-
if (this.heap.length === 2) {
20-
return this.heap.pop();
21-
}
22-
let res;
23-
res = this.heap[1];
24-
this.heap[1] = this.heap.pop();
25-
this.downheap(1, this.heap.length - 1);
26-
return res;
27-
}
28-
downheap(pos, len) {
29-
let tmp, i;
30-
tmp = this.heap[pos];
31-
while (pos <= parseInt(len / 2)) {
32-
i = pos * 2;
33-
if (i < len && this.heap[i][1] < this.heap[i + 1][1]) i++;
34-
if (tmp[1] <= this.heap[i][1]) break;
35-
this.heap[pos] = this.heap[i];
36-
pos = i;
37-
}
38-
this.heap[pos] = tmp;
39-
}
40-
size() {
41-
return this.heap.length - 1;
42-
}
43-
top() {
44-
return this.heap[1];
45-
}
46-
}
47-
function solution(N, road, K) {
48-
let answer = 0;
49-
let minH = new minHeap();
50-
let distance = Array(N + 1).fill(1e9);
51-
let graph = Array(N + 1);
52-
for (let i = 0; i < graph.length; i++) {
53-
graph[i] = Array();
54-
}
55-
56-
for (let [a, b, c] of road) {
1+
function solution(n, edges, k) {
2+
let answer;
3+
let graph = Array(n + 1);
4+
for (let i = 0; i < graph.length; i++) graph[i] = Array();
5+
let check = Array(n + 1).fill(0);
6+
let dist = Array(n + 1).fill(1e9);
7+
edges.forEach(([a, b, c]) => {
578
graph[a].push([b, c]);
58-
graph[b].push([a, c]);
59-
}
9+
});
6010

61-
minH.insert([1, 0]);
62-
distance[1] = 0;
63-
while (minH.size() > 0) {
64-
let [now, time] = minH.get();
65-
if (time > distance[now]) continue;
66-
for (let [next, nextTime] of graph[now]) {
67-
if (time + nextTime < distance[next]) {
68-
distance[next] = time + nextTime;
69-
minH.insert([next, distance[next]]);
70-
}
11+
dist[1] = 0;
12+
for (let i = 1; i <= n; i++) {
13+
let min = 0;
14+
15+
// min을 이용해서 찾아야하는 노드를 min에 입력
16+
for (let j = 1; j <= n; j++) {
17+
if (check[j] === 0 && dist[j] < dist[min]) min = j;
7118
}
72-
}
7319

74-
for (let x of distance) {
75-
if (x <= K) answer++;
20+
// min과 연결된 노드들 탐색
21+
for (let [next, cost] of graph[min]) {
22+
if (dist[min] + cost < dist[next]) {
23+
dist[next] = dist[min] + cost;
24+
}
25+
}
26+
check[min] = 1;
7627
}
77-
return answer;
28+
return dist[k];
7829
}
7930

8031
console.log(
8132
solution(
82-
5,
33+
6,
8334
[
84-
[1, 2, 1],
85-
[2, 3, 3],
86-
[5, 2, 2],
87-
[1, 4, 2],
88-
[5, 3, 1],
89-
[5, 4, 2],
35+
[1, 2, 12],
36+
[1, 3, 4],
37+
[2, 1, 2],
38+
[2, 3, 5],
39+
[2, 5, 5],
40+
[3, 4, 5],
41+
[4, 2, 2],
42+
[4, 5, 5],
43+
[6, 4, 5],
9044
],
91-
3
45+
5
9246
)
93-
); // 4
47+
);
48+
// 14

boj/1654_랜선자르기.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { count } = require('console');
2+
const fs = require('fs');
3+
const filePath =
4+
process.platform === 'linux' ? '/dev/stdin' : './boj/input.txt';
5+
let input = fs.readFileSync(filePath).toString().trim().split('\n');
6+
let [n, m] = input[0].trim().split(' ').map(Number);
7+
let arr = [];
8+
for (let i = 1; i < input.length; i++) {
9+
arr.push(Number(input[i].trim()));
10+
}
11+
function solution(n, m, arr) {
12+
function count(mid) {
13+
let cnt = 0;
14+
for (let x of arr) {
15+
cnt += Math.floor(x / mid);
16+
}
17+
return cnt;
18+
}
19+
let answer = 0;
20+
let left = 1,
21+
right = 10e10;
22+
while (left <= right) {
23+
let mid = parseInt((left + right) / 2);
24+
if (count(mid) >= m) {
25+
answer = mid;
26+
left = mid + 1;
27+
} else right = mid - 1;
28+
}
29+
30+
return answer;
31+
}
32+
console.log(solution(n, m, arr)); // 200

boj/1931_회의실배정.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs');
2+
const filePath =
3+
process.platform === 'linux' ? '/dev/stdin' : './boj/input.txt';
4+
let input = fs.readFileSync(filePath).toString().trim().split('\n');
5+
6+
let n = Number(input[0].trim());
7+
let work = [];
8+
for (let i = 1; i < input.length; i++) {
9+
work.push(input[i].trim().split(' ').map(Number));
10+
}
11+
12+
function solution(n, work) {
13+
let answer = 0;
14+
15+
// 회의가 일찍 끝나는 기준으로 오름차순 정렬을 해야지 더 많은 선택을 할 수 있다.
16+
// 시작 시간과 끝나는 시간이 같은 경우가 있다. 그래서 같을때는 내림차순 정렬을 해줘야 포함이 된다.
17+
work.sort((a, b) => {
18+
if (a[1] === b[1]) return a[0] - b[0];
19+
return a[1] - b[1];
20+
});
21+
22+
let left = 0;
23+
for (let x of work) {
24+
if (x[0] >= left) {
25+
left = x[1];
26+
answer++;
27+
}
28+
}
29+
return answer;
30+
}
31+
console.log(solution(n, work)); // 4

boj/1940_주몽.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { count } = require('console');
2+
const fs = require('fs');
3+
const filePath =
4+
process.platform === 'linux' ? '/dev/stdin' : './boj/input.txt';
5+
let input = fs.readFileSync(filePath).toString().trim().split('\n');
6+
let [n] = input[0].trim().split(' ').map(Number);
7+
let [m] = input[1].trim().split(' ').map(Number);
8+
let temp = [];
9+
let arr = [];
10+
for (let i = 2; i < input.length; i++) {
11+
temp.push(input[i].trim().split(' ').map(Number));
12+
}
13+
for (let x of temp[0]) arr.push(x);
14+
// console.log(n, m, arr);
15+
16+
function solution(n, m, arr) {
17+
let answer = 0;
18+
let left = 0,
19+
right = n - 1;
20+
arr.sort((a, b) => a - b);
21+
22+
// 같으면 안되는 경우가 있기 때문에 같다를 빼줘야 한다. 반례는 아래에 있다.
23+
while (left < right) {
24+
if (arr[left] + arr[right] === m) {
25+
answer++;
26+
left++;
27+
right--;
28+
} else if (arr[left] + arr[right] < m) left++;
29+
else right--;
30+
}
31+
return answer;
32+
}
33+
console.log(solution(n, m, arr));
34+
// 4
35+
// 3
36+
// 1 2 3

boj/19598_최소회의실개수.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fs = require('fs');
2+
const filePath =
3+
process.platform === 'linux' ? '/dev/stdin' : './boj/input.txt';
4+
let input = fs.readFileSync(filePath).toString().trim().split('\n');
5+
6+
let n = Number(input[0].trim());
7+
let work = [];
8+
for (let i = 1; i < input.length; i++) {
9+
work.push(input[i].trim().split(' ').map(Number));
10+
}
11+
12+
// 최소로 사용 가능한 회의실을 구해야된다 -> 최대 겹치는 회의실을 구해야 한다.
13+
function solution(n, work) {
14+
let answer = 0;
15+
let arr = [];
16+
for (let i = 0; i < work.length; i++) {
17+
arr.push([work[i][0], 1]);
18+
arr.push([work[i][1], -1]);
19+
}
20+
21+
// 종료시간을 오름차순 해줘서 끝나는 회의를 cnt에서 먼저 빼줘야 한다.
22+
arr.sort((a, b) => {
23+
if (a[0] === b[0]) return a[1] - b[1];
24+
return a[0] - b[0];
25+
});
26+
27+
let cnt = 0;
28+
for (let [a, b] of arr) {
29+
cnt += b;
30+
answer = Math.max(answer, cnt);
31+
}
32+
return answer;
33+
}
34+
console.log(solution(n, work)); // 4

boj/2110_공유기설치.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { count } = require('console');
2+
const fs = require('fs');
3+
const filePath =
4+
process.platform === 'linux' ? '/dev/stdin' : './boj/input.txt';
5+
let input = fs.readFileSync(filePath).toString().trim().split('\n');
6+
let [n, m] = input[0].trim().split(' ').map(Number);
7+
let arr = [];
8+
for (let i = 1; i < input.length; i++) {
9+
arr.push(Number(input[i].trim()));
10+
}
11+
12+
function solution(n, m, arr) {
13+
let answer = 0;
14+
arr.sort((a, b) => a - b);
15+
16+
return answer;
17+
}
18+
console.log(solution(n, m, arr)); // 200

0 commit comments

Comments
 (0)