Skip to content

Commit ab459f1

Browse files
committed
Add two more problems
1 parent 1e5234a commit ab459f1

File tree

2 files changed

+468
-0
lines changed

2 files changed

+468
-0
lines changed
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
use std::collections::HashSet;
2+
3+
// 1135. Connecting Cities With Minimum Cost, Medium
4+
// https://leetcode.com/problems/connecting-cities-with-minimum-cost/
5+
impl Solution {
6+
pub fn minimum_cost(n: i32, connections: Vec<Vec<i32>>) -> i32 {
7+
// kruskal
8+
let mut union = (0..n as usize).collect::<Vec<usize>>();
9+
10+
fn is_union(union: &Vec<usize>, mut x: usize, mut y: usize) -> bool {
11+
while x != union[x] {
12+
x = union[x];
13+
}
14+
while y != union[y] {
15+
y = union[y];
16+
}
17+
x == y
18+
}
19+
20+
fn find_root(union: &Vec<usize>, mut x: usize) -> usize {
21+
while x != union[x] {
22+
x = union[x];
23+
}
24+
25+
x
26+
}
27+
28+
fn compress_union(a: usize, b: usize, union: &mut Vec<usize>) {
29+
let a = union[a];
30+
let b = union[b];
31+
for i in 0..union.len() {
32+
if union[i] == a {
33+
union[i] = b;
34+
}
35+
}
36+
}
37+
38+
let mut weights = connections.clone(); // [x_i, y_i, cost_i]
39+
weights.sort_unstable_by(|a, b| a[2].cmp(&b[2]));
40+
41+
let mut costs = 0;
42+
for weight in weights {
43+
let [x, y, cost] = [weight[0] as usize - 1, weight[1] as usize - 1, weight[2] as usize];
44+
if !is_union(&union, x, y) {
45+
let root = find_root(&union, x);
46+
compress_union(x, root, &mut union);
47+
compress_union(y, root, &mut union);
48+
costs += cost;
49+
}
50+
}
51+
52+
let unions = union.iter().collect::<HashSet<&usize>>().len();
53+
if unions == 1 {
54+
costs as i32
55+
} else {
56+
-1
57+
}
58+
}
59+
}
60+
61+
struct Solution {}
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
use crate::vec_vec_i32;
67+
68+
#[test]
69+
fn test_minimum_cost() {
70+
assert_eq!(Solution::minimum_cost(3, vec_vec_i32![[1, 2, 5], [1, 3, 6], [2, 3, 1]]), 6);
71+
}
72+
73+
#[test]
74+
fn test_minimum_cost2() {
75+
assert_eq!(Solution::minimum_cost(4, vec_vec_i32![[1, 2, 3], [3, 4, 4]]), -1);
76+
}
77+
78+
#[test]
79+
fn test_minimum_cost3() {
80+
assert_eq!(
81+
Solution::minimum_cost(
82+
50,
83+
vec_vec_i32![
84+
[2, 1, 22135],
85+
[3, 1, 13746],
86+
[4, 3, 37060],
87+
[5, 2, 48513],
88+
[6, 3, 49607],
89+
[7, 1, 97197],
90+
[8, 2, 95909],
91+
[9, 2, 82668],
92+
[10, 2, 48372],
93+
[11, 4, 17775],
94+
[12, 2, 6017],
95+
[13, 1, 51409],
96+
[14, 2, 12884],
97+
[15, 7, 98902],
98+
[16, 14, 52361],
99+
[17, 8, 11588],
100+
[18, 12, 86814],
101+
[19, 17, 49581],
102+
[20, 4, 41808],
103+
[21, 11, 77039],
104+
[22, 10, 80279],
105+
[23, 16, 61659],
106+
[24, 12, 89390],
107+
[25, 24, 10042],
108+
[26, 12, 78278],
109+
[27, 15, 30756],
110+
[28, 6, 2883],
111+
[29, 8, 3478],
112+
[30, 7, 29321],
113+
[31, 12, 47542],
114+
[32, 20, 35806],
115+
[33, 3, 26531],
116+
[34, 12, 16321],
117+
[35, 27, 82484],
118+
[36, 7, 55920],
119+
[37, 24, 21253],
120+
[38, 23, 90537],
121+
[39, 7, 83795],
122+
[40, 36, 70353],
123+
[41, 34, 76983],
124+
[42, 14, 63416],
125+
[43, 15, 39590],
126+
[44, 9, 86794],
127+
[45, 3, 31968],
128+
[46, 19, 32695],
129+
[47, 17, 40287],
130+
[48, 1, 27993],
131+
[49, 12, 86349],
132+
[50, 11, 52080],
133+
[17, 27, 65829],
134+
[42, 45, 87517],
135+
[14, 23, 96130],
136+
[5, 50, 3601],
137+
[10, 17, 2017],
138+
[26, 44, 4118],
139+
[26, 29, 93146],
140+
[1, 9, 56934],
141+
[22, 43, 5984],
142+
[3, 22, 13404],
143+
[13, 28, 66475],
144+
[11, 14, 93296],
145+
[16, 44, 71637],
146+
[7, 37, 88398],
147+
[7, 29, 56056],
148+
[2, 34, 79170],
149+
[40, 44, 55496],
150+
[35, 46, 14494],
151+
[32, 34, 25143],
152+
[28, 36, 59961],
153+
[10, 49, 58317],
154+
[8, 38, 33783],
155+
[8, 28, 19762],
156+
[34, 41, 69590],
157+
[27, 37, 26831],
158+
[15, 23, 53060],
159+
[5, 11, 7570],
160+
[20, 42, 98814],
161+
[18, 34, 96014],
162+
[13, 43, 94702],
163+
[1, 46, 18873],
164+
[44, 45, 43666],
165+
[22, 40, 69729],
166+
[4, 25, 28548],
167+
[8, 46, 19305],
168+
[15, 22, 39749],
169+
[33, 48, 43826],
170+
[14, 15, 38867],
171+
[13, 22, 56073],
172+
[3, 46, 51377],
173+
[13, 15, 73530],
174+
[6, 36, 67511],
175+
[27, 38, 76774],
176+
[6, 21, 21673],
177+
[28, 49, 72219],
178+
[40, 50, 9568],
179+
[31, 37, 66173],
180+
[14, 29, 93641],
181+
[4, 40, 87301],
182+
[18, 46, 41318],
183+
[2, 8, 25717],
184+
[1, 7, 3006],
185+
[9, 22, 85003],
186+
[14, 45, 33961],
187+
[18, 28, 56248],
188+
[1, 31, 10007],
189+
[3, 24, 23971],
190+
[6, 28, 24448],
191+
[35, 39, 87474],
192+
[10, 50, 3371],
193+
[7, 18, 26351],
194+
[19, 41, 86238],
195+
[3, 8, 73207],
196+
[11, 34, 75438],
197+
[3, 47, 35394],
198+
[27, 32, 69991],
199+
[6, 40, 87955],
200+
[2, 18, 85693],
201+
[5, 37, 50456],
202+
[8, 20, 59182],
203+
[16, 38, 58363],
204+
[9, 39, 58494],
205+
[39, 43, 73017],
206+
[10, 15, 88526],
207+
[16, 23, 48361],
208+
[4, 28, 59995],
209+
[2, 3, 66426],
210+
[6, 17, 29387],
211+
[15, 38, 80738],
212+
[12, 43, 63014],
213+
[9, 11, 90635],
214+
[12, 20, 36051],
215+
[13, 25, 1515],
216+
[32, 40, 72665],
217+
[10, 40, 85644],
218+
[13, 40, 70642],
219+
[12, 24, 88771],
220+
[14, 46, 79583],
221+
[30, 49, 45432],
222+
[21, 34, 95097],
223+
[25, 48, 96934],
224+
[2, 35, 79611],
225+
[9, 26, 71147],
226+
[11, 37, 57109],
227+
[35, 36, 67266],
228+
[42, 43, 15913],
229+
[3, 30, 44704],
230+
[4, 32, 46266],
231+
[5, 10, 94508],
232+
[31, 39, 45742],
233+
[12, 25, 56618],
234+
[10, 45, 79396],
235+
[15, 28, 78005],
236+
[19, 32, 94010],
237+
[36, 46, 4417],
238+
[6, 35, 7762],
239+
[10, 13, 12161],
240+
[49, 50, 60013],
241+
[20, 23, 6891],
242+
[9, 50, 63893],
243+
[35, 43, 74832],
244+
[10, 24, 3562],
245+
[6, 8, 47831],
246+
[29, 32, 82689],
247+
[7, 47, 71961],
248+
[14, 41, 82402],
249+
[20, 33, 38732],
250+
[16, 26, 24131],
251+
[17, 34, 96267],
252+
[21, 46, 81067],
253+
[19, 47, 41426],
254+
[13, 24, 68768],
255+
[1, 25, 78243],
256+
[2, 27, 77645],
257+
[11, 25, 96335],
258+
[31, 45, 30726],
259+
[43, 44, 34801],
260+
[3, 42, 22953],
261+
[12, 23, 34898],
262+
[37, 43, 32324],
263+
[18, 44, 18539],
264+
[8, 13, 59737],
265+
[28, 37, 67994],
266+
[13, 14, 25013],
267+
[22, 41, 25671],
268+
[1, 6, 57657],
269+
[8, 11, 83932],
270+
[42, 48, 24122],
271+
[4, 15, 851],
272+
[9, 29, 70508],
273+
[7, 32, 53629],
274+
[3, 4, 34945],
275+
[2, 32, 64478],
276+
[7, 30, 75022],
277+
[14, 19, 55721],
278+
[20, 22, 84838],
279+
[22, 25, 6103],
280+
[8, 49, 11497],
281+
[11, 32, 22278],
282+
[35, 44, 56616],
283+
[12, 49, 18681],
284+
[18, 43, 56358],
285+
[24, 43, 13360],
286+
[24, 47, 59846],
287+
[28, 43, 36311],
288+
[17, 25, 63309],
289+
[1, 14, 30207],
290+
[39, 48, 22241],
291+
[13, 26, 94146],
292+
[4, 33, 62994],
293+
[40, 48, 32450],
294+
[8, 19, 8063],
295+
[20, 29, 56772],
296+
[10, 27, 21224],
297+
[24, 30, 40328],
298+
[44, 46, 48426],
299+
[22, 45, 39752],
300+
[6, 43, 96892],
301+
[2, 30, 73566],
302+
[26, 36, 43360],
303+
[34, 36, 51956],
304+
[18, 20, 5710],
305+
[7, 22, 72496],
306+
[3, 39, 9207],
307+
[15, 30, 39474],
308+
[11, 35, 82661],
309+
[12, 50, 84860],
310+
[14, 26, 25992],
311+
[16, 39, 33166],
312+
[25, 41, 11721],
313+
[19, 40, 68623],
314+
[27, 28, 98119],
315+
[19, 43, 3644],
316+
[8, 16, 84611],
317+
[33, 42, 52972],
318+
[29, 36, 60307],
319+
[9, 36, 44224],
320+
[9, 48, 89857],
321+
[25, 26, 21705],
322+
[29, 33, 12562],
323+
[5, 34, 32209],
324+
[9, 16, 26285],
325+
[22, 37, 80956],
326+
[18, 35, 51968],
327+
[37, 49, 36399],
328+
[18, 42, 37774],
329+
[1, 30, 24687],
330+
[23, 43, 55470],
331+
[6, 47, 69677],
332+
[21, 39, 6826],
333+
[15, 24, 38561]
334+
]
335+
),
336+
616330
337+
);
338+
}
339+
}

0 commit comments

Comments
 (0)