@@ -88,20 +88,21 @@ tags:
88
88
``` python
89
89
class Solution :
90
90
def makeConnected (self , n : int , connections : List[List[int ]]) -> int :
91
- def find (x ) :
91
+ def find (x : int ) -> int :
92
92
if p[x] != x:
93
93
p[x] = find(p[x])
94
94
return p[x]
95
95
96
- cnt, size = 0 , n
96
+ cnt = 0
97
97
p = list (range (n))
98
98
for a, b in connections:
99
- if find(a) == find(b):
99
+ pa, pb = find(a), find(b)
100
+ if pa == pb:
100
101
cnt += 1
101
102
else :
102
- p[find(a) ] = find(b)
103
- size -= 1
104
- return - 1 if size - 1 > cnt else size - 1
103
+ p[pa ] = pb
104
+ n -= 1
105
+ return - 1 if n - 1 > cnt else n - 1
105
106
```
106
107
107
108
#### Java
@@ -117,12 +118,11 @@ class Solution {
117
118
}
118
119
int cnt = 0 ;
119
120
for (int [] e : connections) {
120
- int a = e[0 ];
121
- int b = e[1 ];
122
- if (find(a) == find(b)) {
121
+ int pa = find(e[0 ]), pb = find(e[1 ]);
122
+ if (pa == pb) {
123
123
++ cnt;
124
124
} else {
125
- p[find(a) ] = find(b) ;
125
+ p[pa ] = pb ;
126
126
-- n;
127
127
}
128
128
}
@@ -143,27 +143,26 @@ class Solution {
143
143
``` cpp
144
144
class Solution {
145
145
public:
146
- vector<int > p;
147
-
148
146
int makeConnected(int n, vector<vector<int >>& connections) {
149
- p.resize (n);
150
- for (int i = 0; i < n; ++i) p[i] = i ;
147
+ vector< int > p (n);
148
+ iota(p.begin(), p.end(), 0) ;
151
149
int cnt = 0;
152
- for (auto& e : connections) {
153
- int a = e[0], b = e[1];
154
- if (find(a) == find(b))
150
+ function<int(int)> find = [ &] (int x) -> int {
151
+ if (p[ x] != x) {
152
+ p[ x] = find(p[ x] );
153
+ }
154
+ return p[ x] ;
155
+ };
156
+ for (const auto& c : connections) {
157
+ int pa = find(c[ 0] ), pb = find(c[ 1] );
158
+ if (pa == pb) {
155
159
++cnt;
156
- else {
157
- p[find(a) ] = find(b) ;
160
+ } else {
161
+ p[ pa ] = pb ;
158
162
--n;
159
163
}
160
164
}
161
- return n - 1 > cnt ? -1 : n - 1 ;
162
- }
163
-
164
- int find (int x) {
165
- if (p[ x] != x) p[ x] = find(p[ x] );
166
- return p[ x] ;
165
+ return cnt >= n - 1 ? n - 1 : -1;
167
166
}
168
167
};
169
168
```
@@ -185,11 +184,11 @@ func makeConnected(n int, connections [][]int) int {
185
184
return p[x]
186
185
}
187
186
for _, e := range connections {
188
- a, b := e[0], e[1]
189
- if find(a) == find(b) {
187
+ pa, pb := find( e[0]), find( e[1])
188
+ if pa == pb {
190
189
cnt++
191
190
} else {
192
- p[find(a) ] = find(b)
191
+ p[pa ] = pb
193
192
n--
194
193
}
195
194
}
@@ -200,6 +199,31 @@ func makeConnected(n int, connections [][]int) int {
200
199
}
201
200
```
202
201
202
+ #### TypeScript
203
+
204
+ ``` ts
205
+ function makeConnected(n : number , connections : number [][]): number {
206
+ const p: number [] = Array .from ({ length: n }, (_ , i ) => i );
207
+ const find = (x : number ): number => {
208
+ if (p [x ] !== x ) {
209
+ p [x ] = find (p [x ]);
210
+ }
211
+ return p [x ];
212
+ };
213
+ let cnt = 0 ;
214
+ for (const [a, b] of connections ) {
215
+ const [pa, pb] = [find (a ), find (b )];
216
+ if (pa === pb ) {
217
+ ++ cnt ;
218
+ } else {
219
+ p [pa ] = pb ;
220
+ -- n ;
221
+ }
222
+ }
223
+ return cnt >= n - 1 ? n - 1 : - 1 ;
224
+ }
225
+ ```
226
+
203
227
<!-- tabs: end -->
204
228
205
229
<!-- solution: end -->
0 commit comments