Skip to content

Commit ec562bb

Browse files
authored
Update Find the City With the Smallest Number of Neighbors at a Threshold Distance.java
1 parent 2e85346 commit ec562bb

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

Medium/Find the City With the Smallest Number of Neighbors at a Threshold Distance.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
class Solution {
22
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
3-
Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
3+
Map<Integer, List<int[]>> graph = new HashMap<>();
44
for (int[] edge : edges) {
5-
map.computeIfAbsent(edge[0], k -> new HashMap<>()).put(edge[1], edge[2]);
6-
map.computeIfAbsent(edge[1], k -> new HashMap<>()).put(edge[0], edge[2]);
5+
graph.computeIfAbsent(edge[0], k -> new ArrayList<>())
6+
.add(new int[]{edge[1], edge[2]});
7+
graph.computeIfAbsent(edge[1], k -> new ArrayList<>())
8+
.add(new int[]{edge[0], edge[2]});
79
}
8-
int min = n + 1;
9-
int res = -1;
10+
int minNeighborCount = n + 1;
11+
int nodeWithMinNeighbor = -1;
1012
for (int i = 0; i < n; i++) {
11-
Queue<int[]> queue = new PriorityQueue<>((a, b) -> (b[1] - a[1]));
12-
queue.add(new int[]{i, distanceThreshold});
13-
boolean[] visited = new boolean[n];
14-
int count = 0;
15-
while (!queue.isEmpty()) {
16-
int[] city = queue.poll();
17-
if (visited[city[0]]) {
18-
continue;
19-
}
20-
visited[city[0]] = true;
21-
count++;
22-
Map<Integer, Integer> temp = map.getOrDefault(city[0], new HashMap<>());
23-
for (Integer neighbour : temp.keySet()) {
24-
if (!visited[neighbour] && city[1] >= temp.get(neighbour)) {
25-
queue.add(new int[]{neighbour, city[1] - temp.get(neighbour)});
26-
}
27-
}
13+
int numOfNeighbors = findNumberOfNeighborsWithinThresholdDistance(i, distanceThreshold, graph);
14+
minNeighborCount = Math.min(numOfNeighbors, minNeighborCount);
15+
nodeWithMinNeighbor = minNeighborCount == numOfNeighbors ? i : nodeWithMinNeighbor;
16+
}
17+
return nodeWithMinNeighbor;
18+
}
19+
20+
private int findNumberOfNeighborsWithinThresholdDistance(int node, int distanceThreshold, Map<Integer, List<int[]>> graph) {
21+
Queue<int[]> queue = new PriorityQueue<>((a, b) -> (b[1] - a[1]));
22+
Set<Integer> visited = new HashSet<>();
23+
queue.add(new int[]{node, distanceThreshold});
24+
int numOfNeighbors = 0;
25+
while (!queue.isEmpty()) {
26+
int[] removed = queue.remove();
27+
int currNode = removed[0];
28+
int currDistanceThreshold = removed[1];
29+
if (visited.contains(currNode)) {
30+
continue;
2831
}
29-
if (count - 1 <= min) {
30-
min = count - 1;
31-
res = i;
32+
visited.add(currNode);
33+
numOfNeighbors++;
34+
for (int[] neighbor : graph.getOrDefault(currNode, new ArrayList<>())) {
35+
int neighborNode = neighbor[0];
36+
int neighborDistance = neighbor[1];
37+
if (!visited.contains(neighborNode) &&
38+
currDistanceThreshold >= neighborDistance) {
39+
queue.add(new int[]{neighborNode, currDistanceThreshold - neighborDistance});
40+
}
3241
}
3342
}
34-
return res;
43+
return numOfNeighbors;
3544
}
3645
}
37-

0 commit comments

Comments
 (0)