Skip to content

Commit 6ca35a5

Browse files
committed
[level 2] Title: 배달, Time: 4.76 ms, Memory: 35.2 MB -BaekjoonHub
1 parent 3253bfe commit 6ca35a5

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [level 2] 배달 - 12978
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/12978)
4+
5+
### 성능 요약
6+
7+
메모리: 35.2 MB, 시간: 4.76 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > Summer/Winter Coding(~2018)
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 02월 28일 23:51:13
20+
21+
### 문제 설명
22+
23+
<p>N개의 마을로 이루어진 나라가 있습니다. 이 나라의 각 마을에는 1부터 N까지의 번호가 각각 하나씩 부여되어 있습니다. 각 마을은 양방향으로 통행할 수 있는 도로로 연결되어 있는데, 서로 다른 마을 간에 이동할 때는 이 도로를 지나야 합니다. 도로를 지날 때 걸리는 시간은 도로별로 다릅니다. 현재 1번 마을에 있는 음식점에서 각 마을로 음식 배달을 하려고 합니다. 각 마을로부터 음식 주문을 받으려고 하는데, N개의 마을 중에서 K 시간 이하로 배달이 가능한 마을에서만 주문을 받으려고 합니다. 다음은 N = 5, K = 3인 경우의 예시입니다.</p>
24+
25+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/d7779d88-084c-4ffa-ae9f-2a42f97d3bbf/%E1%84%87%E1%85%A2%E1%84%83%E1%85%A1%E1%86%AF_1_uxun8t.png" title="" alt="배달_1_uxun8t.png"></p>
26+
27+
<p>위 그림에서 1번 마을에 있는 음식점은 [1, 2, 4, 5] 번 마을까지는 3 이하의 시간에 배달할 수 있습니다. 그러나 3번 마을까지는 3시간 이내로 배달할 수 있는 경로가 없으므로 3번 마을에서는 주문을 받지 않습니다. 따라서 1번 마을에 있는 음식점이 배달 주문을 받을 수 있는 마을은 4개가 됩니다.<br>
28+
마을의 개수 N, 각 마을을 연결하는 도로의 정보 road, 음식 배달이 가능한 시간 K가 매개변수로 주어질 때, 음식 주문을 받을 수 있는 마을의 개수를 return 하도록 solution 함수를 완성해주세요.</p>
29+
30+
<h5>제한사항</h5>
31+
32+
<ul>
33+
<li>마을의 개수 N은 1 이상 50 이하의 자연수입니다.</li>
34+
<li>road의 길이(도로 정보의 개수)는 1 이상 2,000 이하입니다.</li>
35+
<li>road의 각 원소는 마을을 연결하고 있는 각 도로의 정보를 나타냅니다.</li>
36+
<li>road는 길이가 3인 배열이며, 순서대로 (a, b, c)를 나타냅니다.
37+
38+
<ul>
39+
<li>a, b(1 ≤ a, b ≤ N, a != b)는 도로가 연결하는 두 마을의 번호이며, c(1 ≤ c ≤ 10,000, c는 자연수)는 도로를 지나는데 걸리는 시간입니다.</li>
40+
<li>두 마을 a, b를 연결하는 도로는 여러 개가 있을 수 있습니다.</li>
41+
<li>한 도로의 정보가 여러 번 중복해서 주어지지 않습니다.</li>
42+
</ul></li>
43+
<li>K는 음식 배달이 가능한 시간을 나타내며, 1 이상 500,000 이하입니다.</li>
44+
<li>임의의 두 마을간에 항상 이동 가능한 경로가 존재합니다.</li>
45+
<li>1번 마을에 있는 음식점이 K 이하의 시간에 배달이 가능한 마을의 개수를 return 하면 됩니다.</li>
46+
</ul>
47+
48+
<hr>
49+
50+
<h5>입출력 예</h5>
51+
<table class="table">
52+
<thead><tr>
53+
<th>N</th>
54+
<th>road</th>
55+
<th>K</th>
56+
<th>result</th>
57+
</tr>
58+
</thead>
59+
<tbody><tr>
60+
<td>5</td>
61+
<td>[[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]]</td>
62+
<td>3</td>
63+
<td>4</td>
64+
</tr>
65+
<tr>
66+
<td>6</td>
67+
<td>[[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]]</td>
68+
<td>4</td>
69+
<td>4</td>
70+
</tr>
71+
</tbody>
72+
</table>
73+
<h5>입출력 예 설명</h5>
74+
75+
<p>입출력 예 #1<br>
76+
문제의 예시와 같습니다.</p>
77+
78+
<p>입출력 예 #2<br>
79+
주어진 마을과 도로의 모양은 아래 그림과 같습니다.<br>
80+
<img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/993685f2-6b97-4fe3-85b5-47c085dc1bf3/%E1%84%87%E1%85%A2%E1%84%83%E1%85%A1%E1%86%AF_3_njc7kq.png" title="" alt="배달_3_njc7kq.png"><br>
81+
1번 마을에서 배달에 4시간 이하가 걸리는 마을은 [1, 2, 3, 5] 4개이므로 4를 return 합니다.</p>
82+
83+
84+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
push(value) {
6+
this.heap.push(value);
7+
this.bubbleUp();
8+
}
9+
pop() {
10+
if (this.size() === 1) return this.heap.pop();
11+
const top = this.heap[0];
12+
this.heap[0] = this.heap.pop();
13+
this.bubbleDown();
14+
return top;
15+
}
16+
size() {
17+
return this.heap.length;
18+
}
19+
bubbleUp() {
20+
let index = this.heap.length - 1;
21+
22+
while (index > 0) {
23+
let parent = Math.floor((index - 1) / 2);
24+
if (this.heap[parent][1] <= this.heap[index][1]) {
25+
break;
26+
} else {
27+
[this.heap[parent], this.heap[index]] = [
28+
this.heap[index],
29+
this.heap[parent],
30+
];
31+
index = parent;
32+
}
33+
}
34+
}
35+
bubbleDown() {
36+
let index = 0;
37+
while (index * 2 + 1 < this.heap.length) {
38+
let left = index * 2 + 1,
39+
right = index * 2 + 2,
40+
smallerChild = left;
41+
42+
if (
43+
right < this.heap.length &&
44+
this.heap[right][1] < this.heap[left][1]
45+
) {
46+
smallerChild = right;
47+
}
48+
49+
if (this.heap[index][1] <= this.heap[smallerChild][1]) break;
50+
51+
[this.heap[index], this.heap[smallerChild]] = [
52+
this.heap[smallerChild],
53+
this.heap[index],
54+
];
55+
index = smallerChild;
56+
}
57+
}
58+
}
59+
60+
function dijkstra(start, dist, adj) {
61+
const pq = new MinHeap();
62+
dist[start] = 0; //시작노드 초기화
63+
64+
pq.push([start, 0]);
65+
66+
while (pq.size() > 0) {
67+
const [curr, d] = pq.pop();
68+
if (dist[curr] < d) {
69+
continue;
70+
}
71+
72+
for (const [next, weight] of adj[curr]) {
73+
const nextDist = weight + d;
74+
if (nextDist < dist[next]) {
75+
dist[next] = nextDist;
76+
pq.push([next, nextDist]);
77+
}
78+
}
79+
}
80+
}
81+
82+
function solution(N, road, K) {
83+
const adj = Array.from({ length: N + 1 }, () => []);
84+
for (const [a, b, c] of road) {
85+
adj[a].push([b, c]); //[마을 번호, 걸리는 시간]
86+
adj[b].push([a, c]);
87+
}
88+
89+
const distances = new Array(N + 1).fill(Infinity);
90+
dijkstra(1, distances, adj);
91+
92+
let answer = 0;
93+
for (let i = 0; i < distances.length; i++) {
94+
if (distances[i] <= K) answer++;
95+
}
96+
return answer;
97+
}

0 commit comments

Comments
 (0)