Skip to content

Commit 2277886

Browse files
authored
Update 2492-minimum-score-of-a-path-between-two-cities.js
1 parent c5ce8a5 commit 2277886

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

2492-minimum-score-of-a-path-between-two-cities.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} roads
4+
* @return {number}
5+
*/
6+
const minScore = function(n, roads) {
7+
const g = {}, visited = Array(n + 1).fill(0)
8+
let res = Infinity
9+
for(const [u, v, d] of roads) {
10+
if(g[u] == null) g[u] = []
11+
if(g[v] == null) g[v] = []
12+
13+
g[u].push([v, d])
14+
g[v].push([u, d])
15+
}
16+
17+
dfs(1)
18+
19+
return res
20+
21+
function dfs(node) {
22+
visited[node] = 1
23+
for(const [nxt, dis] of (g[node] || [])) {
24+
res = Math.min(res, dis)
25+
if(visited[nxt] === 0) {
26+
dfs(nxt)
27+
}
28+
}
29+
}
30+
};
31+
32+
// another
33+
134
class UnionFind {
235
constructor() {
336
this.sizes = new Map()

0 commit comments

Comments
 (0)