Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c43c8f

Browse files
update 1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java
1 parent a0004a7 commit 0c43c8f

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

‎1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,22 @@ public int findTheCity(int n, int[][] edges, int distanceThreshold) {
114114
graph[edge[1]].add(new Edge(edge[0], edge[2])); // to
115115
}
116116

117-
int maxNodes = n + 1;
118-
int maxVertex = -1;
117+
int maxNodeVisits = n + 1;
118+
int cityWithLesserNeighbors = -1;
119119
for(int i = 0; i < n; i++){
120+
// Visit all cities within distanceThreshold
120121
int visits = bfs(graph, i, distanceThreshold);
121-
if(visits <= maxNodes){
122-
maxVertex = i;
123-
maxNodes = Math.min(maxNodes, visits);
122+
if(visits <= maxNodeVisits){
123+
cityWithLesserNeighbors = i;
124+
maxNodeVisits = Math.min(maxNodeVisits, visits);
124125
}
125126
}
126127

127-
return maxVertex;
128+
return cityWithLesserNeighbors;
128129
}
129130

130131
// Breadth-first Search (BFS)
131-
// Returns the number of visited nodes
132+
// Returns the number of visited nodes within the given distance threshold
132133
public int bfs(LinkedList<Edge>[] graph, int vertex, int thresh){
133134
// Storage for the explored vertices
134135
Map<Integer,Integer> map = new HashMap<>();
@@ -199,8 +200,7 @@ public int bfs(LinkedList<Edge>[] graph, int vertex, int thresh){
199200
// for (int from = 0; from < n; from++) {
200201
// for (int to = 0; to < n; to++) {
201202
// // Update edge path if detour city is shorter than direct
202-
// if (dp[from][to] > dp[from][detour] + dp[detour][to])
203-
// dp[from][to] = dp[from][detour] + dp[detour][to];
203+
// dp[from][to] = Math.min(dp[from][to], dp[from][detour] + dp[detour][to]);
204204
// }
205205
// }
206206
// }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /