Skip to content

Commit 7b5d631

Browse files
committed
Solve : '배달' 솔루션 코드 추가
1 parent 3d067af commit 7b5d631

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Level2/배달.js

+29
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,32 @@ function solution1(N, road, K) {
3838

3939
return townsCount;
4040
}
41+
42+
// Solution 2
43+
function solution2(N, road, K) {
44+
const graph = Array.from({ length: N + 1 }, () => []);
45+
const townsDistance = Array.from({ length: N + 1 }, () => Infinity);
46+
47+
road.forEach(([x, y, time]) => {
48+
graph[x].push([y, time]);
49+
graph[y].push([x, time]);
50+
});
51+
52+
function dfs(townNum, totalTime) {
53+
if (townsDistance[townNum] < totalTime) {
54+
return;
55+
}
56+
57+
townsDistance[townNum] = totalTime;
58+
59+
for (const [nextTownNum, time] of graph[townNum]) {
60+
dfs(nextTownNum, totalTime + time);
61+
}
62+
}
63+
64+
dfs(1, 0);
65+
66+
const townsCount = townsDistance.filter((time) => time <= K).length;
67+
68+
return townsCount;
69+
}

0 commit comments

Comments
 (0)