This approach involves using the Union-Find data structure (also known as Disjoint Set Union, DSU) to manage connections between cities efficiently. By iterating over all roads, we determine which cities are interconnected. The key is to keep track of the minimum weight of a road that connects these cities after they are all unified.
This solution utilizes an efficient means of finding and unifying elements, reducing the overhead of nested loops and allowing operations near constant-time with path compression and union by rank techniques.
Time Complexity: O(E log* V)
Space Complexity: O(V)
1public class Solution {
2 public class UnionFind {
3 public int[] parent, rank;
4 public UnionFind(int n) {
5 parent = new int[n + 1];
6 rank = new int[n + 1];
7 for (int i = 1; i <= n; i++)
8 parent[i] = i;
9 }
10
11 public int Find(int u) {
12 if (parent[u] != u)
13 parent[u] = Find(parent[u]);
14 return parent[u];
15 }
16
17 public void Union(int u, int v) {
18 int pu = Find(u);
19 int pv = Find(v);
20 if (pu != pv) {
21 if (rank[pu] > rank[pv])
22 parent[pv] = pu;
23 else if (rank[pu] < rank[pv])
24 parent[pu] = pv;
25 else {
26 parent[pv] = pu;
27 rank[pu]++;
28 }
29 }
30 }
31 }
32
33 public int MinScore(int n, int[][] roads) {
34 UnionFind uf = new UnionFind(n);
35 int result = int.MaxValue;
36 foreach (int[] road in roads) {
37 uf.Union(road[0], road[1]);
38 }
39 foreach (int[] road in roads) {
40 if (uf.Find(road[0]) == uf.Find(1) || uf.Find(road[1]) == uf.Find(1))
41 result = Math.Min(result, road[2]);
42 }
43 return result;
44 }
45}
46
This C# solution constructs a Union-Find data structure to unite the roads, and then it scans through connected roads to find the minimal score for connecting city 1 to city n.
Another intuitive approach is to use graph traversal techniques such as BFS or DFS. Starting from city 1, you can explore all reachable cities while dynamically updating the minimum edge encountered during the exploration. This ensures you calculate the smallest score path by evaluating all potential paths to the destination city.
Time Complexity: O(V + E)
Space Complexity: O(V + E)
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 private int DFS(int node, bool[] visited, Dictionary<int, List<int[]>> graph, int currentMin) {
6 visited[node] = true;
7 foreach (var edge in graph[node]) {
8 int adj = edge[0];
9 int dist = edge[1];
10 if (!visited[adj]) {
11 currentMin = Math.Min(currentMin, dist);
12 currentMin = DFS(adj, visited, graph, currentMin);
13 }
14 }
15 return currentMin;
16 }
17
18 public int MinScore(int n, int[][] roads) {
19 var graph = new Dictionary<int, List<int[]>>();
20 for (int i = 1; i <= n; i++)
21 graph[i] = new List<int[]>();
22 foreach (var road in roads) {
23 graph[road[0]].Add(new int[]{road[1], road[2]});
24 graph[road[1]].Add(new int[]{road[0], road[2]});
25 }
26 var visited = new bool[n + 1];
27 return DFS(1, visited, graph, int.MaxValue);
28 }
29}
30
The C# DFS approach uses a Dictionary to create an adjacency list of cities and applies depth-first search starting from city 1 to find and track the smallest valid connecting edge to city n.