Skip to content

Commit 3df645c

Browse files
authored
Merge pull request #702 from eric-hjh/main
[황장현] 107차 라이브 코테 제출
2 parents 3d3976a + ea22e8f commit 3df645c

File tree

4 files changed

+160
-2
lines changed

4 files changed

+160
-2
lines changed

.github/workflows/merge-and-generate-problems.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ jobs:
8585
- name: Create Next Test Folder
8686
if: env.pr_found == 'true'
8787
run: |
88-
mkdir -p $LIVE_FOLDER/$NEXT_TEST/{문제1,문제2,문제3}
88+
mkdir -p $LIVE_FOLDER/$NEXT_TEST/{문제1,문제2}
8989
touch $LIVE_FOLDER/$NEXT_TEST/문제1/.gitkeep
9090
touch $LIVE_FOLDER/$NEXT_TEST/문제2/.gitkeep
91-
touch $LIVE_FOLDER/$NEXT_TEST/문제3/.gitkeep
9291
echo "✅ Created folder: $LIVE_FOLDER/$NEXT_TEST"
9392
9493
- name: Commit and Push Changes
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
6+
push(node) {
7+
this.heap.push(node);
8+
this.bubbleUp();
9+
}
10+
11+
pop() {
12+
if (this.heap.length === 1) return this.heap.pop();
13+
const min = this.heap[0];
14+
this.heap[0] = this.heap.pop();
15+
this.bubbleDown();
16+
return min;
17+
}
18+
19+
bubbleUp() {
20+
let i = this.heap.length - 1;
21+
while (i > 0) {
22+
const parent = Math.floor((i - 1) / 2);
23+
if (this.heap[parent][0] <= this.heap[i][0]) break;
24+
[this.heap[parent], this.heap[i]] = [this.heap[i], this.heap[parent]];
25+
i = parent;
26+
}
27+
}
28+
29+
bubbleDown() {
30+
let i = 0;
31+
const len = this.heap.length;
32+
while (true) {
33+
const left = 2 * i + 1;
34+
const right = 2 * i + 2;
35+
let smallest = i;
36+
37+
if (left < len && this.heap[left][0] < this.heap[smallest][0])
38+
smallest = left;
39+
if (right < len && this.heap[right][0] < this.heap[smallest][0])
40+
smallest = right;
41+
if (smallest === i) break;
42+
43+
[this.heap[i], this.heap[smallest]] = [this.heap[smallest], this.heap[i]];
44+
i = smallest;
45+
}
46+
}
47+
48+
size() {
49+
return this.heap.length;
50+
}
51+
}
52+
53+
const input = require('fs')
54+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
55+
.toString()
56+
.trim()
57+
.split('\n')
58+
.map((el) => el.split(' ').map(Number));
59+
60+
function solution(input) {
61+
const [V, E] = input[0];
62+
const K = input[1][0];
63+
const edges = input.slice(2);
64+
65+
const graph = Array.from({ length: V + 1 }, () => []);
66+
for (const [u, v, w] of edges) {
67+
graph[u].push([v, w]);
68+
}
69+
70+
const ans = Array(V + 1).fill(Infinity);
71+
ans[K] = 0;
72+
73+
const minHeap = new MinHeap();
74+
minHeap.push([0, K]);
75+
76+
while (minHeap.size()) {
77+
const [dis, node] = minHeap.pop();
78+
if (dis > ans[node]) continue;
79+
80+
for (const [v, w] of graph[node]) {
81+
if (ans[v] > dis + w) {
82+
ans[v] = dis + w;
83+
minHeap.push([ans[v], v]);
84+
}
85+
}
86+
}
87+
88+
return ans
89+
.slice(1)
90+
.map((x) => (x === Infinity ? 'INF' : x))
91+
.join('\n');
92+
}
93+
94+
console.log(solution(input));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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, D] = input[0];
10+
const Info = input.slice(1);
11+
const dp = Array(D + 1).fill(Infinity);
12+
dp[0] = 0;
13+
14+
const graph = Array.from({ length: D + 1 }, () => []);
15+
for (const [start, end, 지름길] of Info) {
16+
if (end > D) continue;
17+
if (end - start <= 지름길) continue;
18+
graph[start].push([end, 지름길]);
19+
}
20+
21+
for (let i = 0; i <= D; i++) {
22+
if (i > 0) dp[i] = Math.min(dp[i], dp[i - 1] + 1);
23+
24+
for (const [end, 지름길] of graph[i]) {
25+
if (dp[end] > dp[i] + 지름길) {
26+
dp[end] = dp[i] + 지름길;
27+
}
28+
}
29+
}
30+
31+
return dp[D];
32+
}
33+
34+
console.log(solution(input));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function solution(n) {
2+
const triangle = Array.from({ length: n }, (_, i) => Array(i + 1).fill(0));
3+
const total = (n * (n + 1)) / 2;
4+
5+
const dx = [1, 0, -1];
6+
const dy = [0, 1, -1];
7+
8+
let x = 0,
9+
y = 0,
10+
dir = 0,
11+
num = 1;
12+
13+
while (num <= total) {
14+
triangle[x][y] = num++;
15+
let nx = x + dx[dir];
16+
let ny = y + dy[dir];
17+
18+
if (nx < 0 || ny < 0 || nx >= n || ny > nx || triangle[nx][ny] !== 0) {
19+
dir = (dir + 1) % 3;
20+
nx = x + dx[dir];
21+
ny = y + dy[dir];
22+
}
23+
24+
x = nx;
25+
y = ny;
26+
}
27+
28+
return triangle.flat();
29+
}
30+
31+
console.log(solution(4));

0 commit comments

Comments
 (0)