|
87 | 87 | // * Votes: 6
|
88 | 88 |
|
89 | 89 |
|
| 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 | + |
90 | 143 | // /* Dikstra Algorithm */
|
91 | 144 | // class Edge {
|
92 | 145 | // int to;
|
|
98 | 151 | // }
|
99 | 152 | // }
|
100 | 153 | //
|
101 |
| -// /* Dijkstra Algorithm */ |
102 | 154 | // class Solution {
|
103 | 155 | // public int findTheCity(int n, int[][] edges, int distanceThreshold) {
|
104 | 156 | // // Create Linked list of edges as the vertex
|
|
172 | 224 | // }
|
173 | 225 | // }
|
174 | 226 |
|
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