Skip to content

Commit ad5b33e

Browse files
add 2492
1 parent 48c006d commit ad5b33e

File tree

3 files changed

+87
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+87
-0
lines changed

paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2520.java) | [:tv:](https://youtu.be/7SHHsS5kPJ8) | Easy ||
2929
| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2515.java) || Easy ||
3030
| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2496.java) || Easy ||
31+
| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2492.java) || Medium |Union Find
3132
| 2467 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java) || Easy ||
3233
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java) || Easy ||
3334
| 2433 | [Find The Original Array of Prefix Xor](https://leetcode.com/problems/find-the-original-array-of-prefix-xor/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2433.java) | [:tv:](https://youtu.be/idcT-p_DDrI) | Medium ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class _2492 {
9+
public static class Solution1 {
10+
public int minScore(int n, int[][] roads) {
11+
UnionFind uf = new UnionFind(n);
12+
//union all roads first
13+
for (int[] road : roads) {
14+
uf.union(road[0], road[1]);
15+
}
16+
//now call find() to completely union all connected cities
17+
for (int i = 1; i <= n; i++) {
18+
uf.find(i);
19+
}
20+
//now we'd like to find all cities that are connected to city 1
21+
Set<Integer> nodes = new HashSet<>();
22+
int startIndex = uf.find(1);
23+
for (int i = 2; i <= n; i++) {
24+
if (uf.find(i) == startIndex) {
25+
nodes.add(i);
26+
}
27+
}
28+
int minScore = Integer.MAX_VALUE;
29+
for (int[] road : roads) {
30+
if (nodes.contains(road[0]) || nodes.contains(road[1])) {
31+
minScore = Math.min(minScore, road[2]);
32+
}
33+
}
34+
return minScore;
35+
}
36+
37+
static class UnionFind {
38+
int[] ids;
39+
40+
public UnionFind(int n) {
41+
this.ids = new int[n + 1];
42+
for (int i = 1; i <= n; i++) {
43+
this.ids[i] = i;
44+
}
45+
}
46+
47+
public int find(int x) {
48+
if (x != ids[x]) {
49+
ids[x] = find(ids[x]);
50+
}
51+
return ids[x];
52+
}
53+
54+
public void union(int x, int y) {
55+
ids[find(x)] = find(y);
56+
}
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2492;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2492Test {
10+
private static _2492.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2492.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(5, solution1.minScore(4, new int[][]{{1, 2, 9}, {2, 3, 6}, {2, 4, 5}, {1, 4, 7}}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1885, solution1.minScore(6, new int[][]{{4, 5, 7468}, {6, 2, 7173}, {6, 3, 8365}, {2, 3, 7674}, {5, 6, 7852}, {1, 2, 8547}, {2, 4, 1885}, {2, 5, 5192}, {1, 3, 4065}, {1, 4, 7357}}));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)