Skip to content

Commit e8ea436

Browse files
add 1466
1 parent bc317f6 commit e8ea436

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1466|[Reorder Routes to Make All Paths Lead to the City Zero](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1466.java) | |Medium|Tree, DFS|
1112
|1464|[Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1464.java) | |Easy|Array|
1213
|1460|[Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1460.java) | |Easy|Array|
1314
|1457|[Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1457.java) | |Medium|Bit Manipulation, Tree, DFS|
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.LinkedList;
6+
import java.util.Map;
7+
import java.util.Queue;
8+
import java.util.Set;
9+
10+
public class _1466 {
11+
public static class Solution1 {
12+
public int minReorder(int n, int[][] connections) {
13+
//key is entering city, value is departure city
14+
Map<Integer, Set<Integer>> map = new HashMap<>();
15+
Queue<Integer> queue = new LinkedList<>();
16+
int minReorder = 0;
17+
Set<Integer> visited = new HashSet<>();
18+
for (int i = 0; i < n; i++) {
19+
visited.add(i);
20+
}
21+
22+
//key is departure city, value is entering city
23+
Map<Integer, Set<Integer>> reverseMap = new HashMap<>();
24+
for (int[] con : connections) {
25+
if (!map.containsKey(con[1])) {
26+
map.put(con[1], new HashSet<>());
27+
}
28+
map.get(con[1]).add(con[0]);
29+
30+
if (!reverseMap.containsKey(con[0])) {
31+
reverseMap.put(con[0], new HashSet<>());
32+
}
33+
reverseMap.get(con[0]).add(con[1]);
34+
35+
//for all those directly connected to city 0, must be reordered if not yet
36+
//and they are the start nodes of BFS
37+
if (con[0] == 0) {
38+
minReorder++;
39+
queue.offer(con[1]);
40+
visited.remove(con[1]);
41+
visited.remove(0);
42+
}
43+
if (con[1] == 0) {
44+
queue.offer(con[0]);
45+
visited.remove(0);
46+
}
47+
}
48+
while (!queue.isEmpty() || !visited.isEmpty()) {
49+
int curr = queue.poll();
50+
visited.remove(curr);
51+
if (map.containsKey(curr)) {
52+
Set<Integer> departureCityList = map.get(curr);
53+
for (int city : departureCityList) {
54+
if (visited.contains(city)) {
55+
queue.offer(city);
56+
}
57+
}
58+
}
59+
if (reverseMap.containsKey(curr)) {
60+
Set<Integer> enteringCityList = reverseMap.get(curr);
61+
for (int city : enteringCityList) {
62+
if (visited.contains(city)) {
63+
queue.offer(city);
64+
minReorder++;
65+
}
66+
}
67+
}
68+
}
69+
return minReorder;
70+
}
71+
}
72+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1466;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1466Test {
10+
private static _1466.Solution1 solution1;
11+
private static int[][] connections;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1466.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
connections = new int[][]{
21+
{0, 1}, {1, 3}, {2, 3}, {4, 0}, {4, 5}
22+
};
23+
assertEquals(3, solution1.minReorder(6, connections));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)