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 212f6e8

Browse files
update 1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java
1 parent 2792800 commit 212f6e8

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

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

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,59 @@
8787
// * Votes: 6
8888

8989

90+
/* Floyd Warshall */
91+
class Solution {
92+
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
93+
// This needs to be a float because it needs to store the Integer.MAX_VALUE.
94+
// Else if this is int, adding a positive number to the max value an integer
95+
// can handle, the bits will overflow and becomes a negative number.
96+
// Alternatively, instead of the MAX_VALUE as a placeholder, since the
97+
// constraint for distanceThreshold <= 10^4, we can initialize it with
98+
// anything greater than the threshold value (i.e., 10001).
99+
float[][] dp = new float[n][n];
100+
101+
// Initialize dp
102+
for (int i = 0; i < n; i++) {
103+
Arrays.fill(dp[i], Integer.MAX_VALUE);
104+
dp[i][i] = 0;
105+
}
106+
107+
for (int[] edge : edges) {
108+
// Fill dp with from to edge grid; dp[from][to] = weight
109+
dp[edge[0]][edge[1]] = edge[2];
110+
dp[edge[1]][edge[0]] = edge[2];
111+
}
112+
113+
// Find all shortest path
114+
for (int detour = 0; detour < n; detour++) {
115+
for (int from = 0; from < n; from++) {
116+
for (int to = 0; to < n; to++) {
117+
// Update edge path if detour city is shorter than direct
118+
dp[from][to] = Math.min(dp[from][to], dp[from][detour] + dp[detour][to]);
119+
}
120+
}
121+
}
122+
123+
int maxVisits = n + 1;
124+
int cityWithLesserNeighbors = -1;
125+
for(int from = 0; from < n; from++) {
126+
// Get all neighboring cities with less than distanceThreshold edge
127+
int neighborCitiesWithinLimit = 0;
128+
for(int to = 0; to < n; to++) {
129+
if(dp[from][to] <= distanceThreshold)
130+
neighborCitiesWithinLimit++;
131+
}
132+
if(neighborCitiesWithinLimit <= maxVisits){
133+
cityWithLesserNeighbors = from;
134+
maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit);
135+
}
136+
}
137+
138+
return cityWithLesserNeighbors;
139+
}
140+
}
141+
142+
90143
// /* Dikstra Algorithm */
91144
// class Edge {
92145
// int to;
@@ -98,7 +151,6 @@
98151
// }
99152
// }
100153
//
101-
// /* Dijkstra Algorithm */
102154
// class Solution {
103155
// public int findTheCity(int n, int[][] edges, int distanceThreshold) {
104156
// // Create Linked list of edges as the vertex
@@ -172,54 +224,3 @@
172224
// }
173225
// }
174226

175-
/* Floyd Warshall */
176-
class Solution {
177-
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
178-
// This needs to be a float because it needs to store the Integer.MAX_VALUE.
179-
// Else if this is int, adding a positive number to the max value an integer
180-
// can handle, the bits will overflow and becomes a negative number.
181-
// Alternatively, instead of the MAX_VALUE as a placeholder, since the
182-
// constraint for distanceThreshold <= 10^4, we can initialize it with
183-
// anything greater than the threshold value (i.e., 10001).
184-
float[][] dp = new float[n][n];
185-
186-
// Initialize dp
187-
for (int i = 0; i < n; i++) {
188-
Arrays.fill(dp[i], Integer.MAX_VALUE);
189-
dp[i][i] = 0;
190-
}
191-
192-
for (int[] edge : edges) {
193-
// Fill dp with from to edge grid; dp[from][to] = weight
194-
dp[edge[0]][edge[1]] = edge[2];
195-
dp[edge[1]][edge[0]] = edge[2];
196-
}
197-
198-
// Find all shortest path
199-
for (int detour = 0; detour < n; detour++) {
200-
for (int from = 0; from < n; from++) {
201-
for (int to = 0; to < n; to++) {
202-
// Update edge path if detour city is shorter than direct
203-
dp[from][to] = Math.min(dp[from][to], dp[from][detour] + dp[detour][to]);
204-
}
205-
}
206-
}
207-
208-
int maxVisits = n + 1;
209-
int cityWithLesserNeighbors = -1;
210-
for(int from = 0; from < n; from++) {
211-
// Get all neighboring cities with less than distanceThreshold edge
212-
int neighborCitiesWithinLimit = 0;
213-
for(int to = 0; to < n; to++) {
214-
if(dp[from][to] <= distanceThreshold)
215-
neighborCitiesWithinLimit++;
216-
}
217-
if(neighborCitiesWithinLimit <= maxVisits){
218-
cityWithLesserNeighbors = from;
219-
maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit);
220-
}
221-
}
222-
223-
return cityWithLesserNeighbors;
224-
}
225-
}

0 commit comments

Comments
(0)

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