Skip to content

Commit 78d7599

Browse files
committed
update 1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java
1 parent 29edce8 commit 78d7599

File tree

1 file changed

+75
-18
lines changed

1 file changed

+75
-18
lines changed

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

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,39 +148,96 @@
148148
// }
149149

150150
/* Floyd Warshall */
151+
152+
// class Solution {
153+
// public int findTheCity(int n, int[][] edges, int distanceThreshold) {
154+
// int[][] dp = new int[n][n];
155+
//
156+
// // Initialize dp
157+
// for (int i = 0; i < n; i++) {
158+
// Arrays.fill(dp[i], Integer.MAX_VALUE);
159+
// dp[i][i] = 0;
160+
// }
161+
//
162+
// for (int[] edge : edges) {
163+
// // Fill dp with from to edge grid; dp[from][to] = weight
164+
// dp[edge[0]][edge[1]] = edge[2];
165+
// dp[edge[1]][edge[0]] = edge[2];
166+
// }
167+
//
168+
// // Find all shortest path
169+
// for (int detour = 0; detour < n; detour++) {
170+
// for (int from = 0; from < n; from++) {
171+
// for (int to = 0; to < n; to++) {
172+
// // Update edge path if detour city is shorter than direct
173+
// if (dp[from][to] > dp[from][detour] + dp[detour][to])
174+
// dp[from][to] = dp[from][detour] + dp[detour][to];
175+
// }
176+
// }
177+
// }
178+
//
179+
// int maxVisits = n + 1;
180+
// int cityWithLesserNeighbors = -1;
181+
// for(int from = 0; from < n; from++) {
182+
// // Get all neighboring cities with less than distanceThreshold edge
183+
// int neighborCitiesWithinLimit = 0;
184+
// for(int to = 0; to < n; to++) {
185+
// if(dp[from][to] <= distanceThreshold)
186+
// neighborCitiesWithinLimit++;
187+
// }
188+
// if(neighborCitiesWithinLimit <= maxVisits){
189+
// cityWithLesserNeighbors = from;
190+
// maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit);
191+
// }
192+
// }
193+
//
194+
// return cityWithLesserNeighbors;
195+
// }
196+
// }
197+
198+
151199
class Solution {
152200
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
153201
float[][] dp = new float[n][n];
154-
for(int i = 0; i < n; i++){
155-
Arrays.fill(dp[i],Integer.MAX_VALUE);
156-
/* If int[][] Arrays.fill(dp[i],12345); */
202+
203+
// Initialize dp
204+
for (int i = 0; i < n; i++) {
205+
Arrays.fill(dp[i], Integer.MAX_VALUE);
157206
dp[i][i] = 0;
158207
}
159-
for(int[] edge : edges){
208+
209+
for (int[] edge : edges) {
210+
// Fill dp with from to edge grid; dp[from][to] = weight
160211
dp[edge[0]][edge[1]] = edge[2];
161212
dp[edge[1]][edge[0]] = edge[2];
162213
}
163214

164-
for(int k = 0; k < n; k++){
165-
for(int i = 0; i < n; i++) {
166-
for(int j = 0; j < n; j++) {
167-
dp[i][j] = Math.min(dp[i][j],dp[i][k] + dp[k][j]);
215+
// Find all shortest path
216+
for (int detour = 0; detour < n; detour++) {
217+
for (int from = 0; from < n; from++) {
218+
for (int to = 0; to < n; to++) {
219+
// Update edge path if detour city is shorter than direct
220+
if (dp[from][to] > dp[from][detour] + dp[detour][to])
221+
dp[from][to] = dp[from][detour] + dp[detour][to];
168222
}
169223
}
170224
}
171-
int maxVisits = n+1;
172-
int result = -1;
173225

174-
for(int i = 0; i < n; i++) {
175-
int visit = 0;
176-
for(int j = 0; j < n; j++) {
177-
if(dp[i][j] <= distanceThreshold) visit++;
226+
int maxVisits = n + 1;
227+
int cityWithLesserNeighbors = -1;
228+
for(int from = 0; from < n; from++) {
229+
// Get all neighboring cities with less than distanceThreshold edge
230+
int neighborCitiesWithinLimit = 0;
231+
for(int to = 0; to < n; to++) {
232+
if(dp[from][to] <= distanceThreshold)
233+
neighborCitiesWithinLimit++;
178234
}
179-
if(visit <= maxVisits){
180-
result = i;
181-
maxVisits = Math.min(maxVisits,visit);
235+
if(neighborCitiesWithinLimit <= maxVisits){
236+
cityWithLesserNeighbors = from;
237+
maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit);
182238
}
183239
}
184-
return result;
240+
241+
return cityWithLesserNeighbors;
185242
}
186243
}

0 commit comments

Comments
 (0)