@@ -114,21 +114,22 @@ public int findTheCity(int n, int[][] edges, int distanceThreshold) {
114
114
graph [edge [1 ]].add (new Edge (edge [0 ], edge [2 ])); // to
115
115
}
116
116
117
- int maxNodes = n + 1 ;
118
- int maxVertex = -1 ;
117
+ int maxNodeVisits = n + 1 ;
118
+ int cityWithLesserNeighbors = -1 ;
119
119
for (int i = 0 ; i < n ; i ++){
120
+ // Visit all cities within distanceThreshold
120
121
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 );
124
125
}
125
126
}
126
127
127
- return maxVertex ;
128
+ return cityWithLesserNeighbors ;
128
129
}
129
130
130
131
// Breadth-first Search (BFS)
131
- // Returns the number of visited nodes
132
+ // Returns the number of visited nodes within the given distance threshold
132
133
public int bfs (LinkedList <Edge >[] graph , int vertex , int thresh ){
133
134
// Storage for the explored vertices
134
135
Map <Integer ,Integer > map = new HashMap <>();
@@ -199,8 +200,7 @@ public int bfs(LinkedList<Edge>[] graph, int vertex, int thresh){
199
200
// for (int from = 0; from < n; from++) {
200
201
// for (int to = 0; to < n; to++) {
201
202
// // 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]);
204
204
// }
205
205
// }
206
206
// }
0 commit comments