|
60 | 60 |
|
61 | 61 | <!-- solution:start -->
|
62 | 62 |
|
63 |
| -### Solution 1 |
| 63 | +### Solution 1: DFS |
| 64 | + |
| 65 | +We create an array $\textit{vis}$ to record whether each city has been visited. |
| 66 | + |
| 67 | +Next, we traverse each city $i$. If the city has not been visited, we start a depth-first search from that city. Using the matrix $\textit{isConnected}$, we find the cities directly connected to this city. These cities and the current city belong to the same province. We continue the depth-first search for these cities until all cities in the same province have been visited. This counts as one province, so we increment the answer $\textit{ans}$ by $1$. Then, we move to the next unvisited city and repeat the process until all cities have been traversed. |
| 68 | + |
| 69 | +Finally, return the answer. |
| 70 | + |
| 71 | +The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the number of cities. |
64 | 72 |
|
65 | 73 | <!-- tabs:start -->
|
66 | 74 |
|
@@ -127,7 +135,7 @@ public:
|
127 | 135 | int ans = 0;
|
128 | 136 | bool vis[n];
|
129 | 137 | memset(vis, false, sizeof(vis));
|
130 |
| - function<void(int)> dfs = [&](int i) { |
| 138 | + auto dfs = [&](this auto&& dfs, int i) -> void { |
131 | 139 | vis[i] = true;
|
132 | 140 | for (int j = 0; j < n; ++j) {
|
133 | 141 | if (!vis[j] && isConnected[i][j]) {
|
@@ -232,7 +240,15 @@ impl Solution {
|
232 | 240 |
|
233 | 241 | <!-- solution:start -->
|
234 | 242 |
|
235 |
| -### Solution 2 |
| 243 | +### Solution 2: Union-Find |
| 244 | + |
| 245 | +We can also use the union-find data structure to maintain each connected component. Initially, each city belongs to a different connected component, so the number of provinces is $n$. |
| 246 | + |
| 247 | +Next, we traverse the matrix $\textit{isConnected}$. If there is a connection between two cities $(i, j)$ and they belong to two different connected components, they will be merged into one connected component, and the number of provinces is decremented by $1$. |
| 248 | + |
| 249 | +Finally, return the number of provinces. |
| 250 | + |
| 251 | +The time complexity is $O(n^2 \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of cities, and $\log n$ is the time complexity of path compression in the union-find data structure. |
236 | 252 |
|
237 | 253 | <!-- tabs:start -->
|
238 | 254 |
|
@@ -304,7 +320,7 @@ public:
|
304 | 320 | int n = isConnected.size();
|
305 | 321 | int p[n];
|
306 | 322 | iota(p, p + n, 0);
|
307 |
| - function<int(int)> find = [&](int x) -> int { |
| 323 | + auto find = [&](this auto&& find, int x) -> int { |
308 | 324 | if (p[x] != x) {
|
309 | 325 | p[x] = find(p[x]);
|
310 | 326 | }
|
@@ -364,10 +380,7 @@ func findCircleNum(isConnected [][]int) (ans int) {
|
364 | 380 | ```ts
|
365 | 381 | function findCircleNum(isConnected: number[][]): number {
|
366 | 382 | const n = isConnected.length;
|
367 |
| - const p: number[] = new Array(n); |
368 |
| - for (let i = 0; i < n; ++i) { |
369 |
| - p[i] = i; |
370 |
| - } |
| 383 | + const p: number[] = Array.from({ length: n }, (_, i) => i); |
371 | 384 | const find = (x: number): number => {
|
372 | 385 | if (p[x] !== x) {
|
373 | 386 | p[x] = find(p[x]);
|
|
0 commit comments