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 a0004a7

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

File tree

1 file changed

+125
-101
lines changed

1 file changed

+125
-101
lines changed
Lines changed: 125 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import java.util.Arrays;
1+
import java.util.HashMap;
2+
import java.util.LinkedList;
3+
import java.util.Map;
4+
import java.util.PriorityQueue;
25

36
/*
47
* @lc app=leetcode id=1334 lang=java
@@ -84,118 +87,139 @@
8487
// * Votes: 6
8588

8689

87-
// class Edge {
88-
// int to;
89-
// int weight;
90-
//
91-
// public Edge(int to, int weight){
92-
// this.to = to;
93-
// this.weight = weight;
94-
// }
95-
//
96-
// }
97-
//
98-
// /* Dijkstra Algorithm */
99-
// class Solution {
100-
// public int findTheCity(int n, int[][] edges, int distanceThreshold) {
101-
//
102-
// LinkedList<Edge>[] graph = new LinkedList[n];
103-
//
104-
// for(int i = 0; i < graph.length; i++){
105-
// graph[i] = new LinkedList<>();
106-
// }
107-
// for(int[] edge : edges){
108-
// graph[edge[0]].add(new Edge(edge[1],edge[2]));
109-
// graph[edge[1]].add(new Edge(edge[0],edge[2]));
110-
// }
111-
//
112-
// int maxVertex = -1;
113-
// int maxNodes = n+1;
114-
// for(int i = 0; i < n; i++){
115-
// int visits = bfs(graph,i,distanceThreshold);
116-
// if(visits <= maxNodes){
117-
// maxVertex = i;
118-
// maxNodes = Math.min(maxNodes,visits);
119-
// }
120-
// }
121-
//
122-
// return maxVertex;
123-
// }
124-
//
125-
// public int bfs(LinkedList<Edge>[] graph, int vertex, int thresh){
126-
// Map<Integer,Integer> map = new HashMap<>();
127-
//
128-
// PriorityQueue<Edge> pq = new PriorityQueue<>((Edge a, Edge b) -> (a.weight - b.weight));
129-
// pq.offer(new Edge(vertex,0));
130-
//
131-
// while(!pq.isEmpty()){
132-
// Edge edge = pq.remove();
133-
// if(map.containsKey(edge.to) && edge.weight > map.get(edge.to)) continue;
134-
// map.put(edge.to,edge.weight);
135-
//
136-
// for(Edge e : graph[edge.to]){
137-
// int dist = e.weight + edge.weight;
138-
// if(dist > thresh) continue;
139-
// if(!map.containsKey(e.to) || (map.get(e.to) > dist)){
140-
// map.put(e.to,dist);
141-
// pq.offer(new Edge(e.to,dist));
142-
// }
143-
// }
144-
// }
145-
//
146-
// return map.size() - 1;
147-
// }
148-
// }
90+
/* Dikstra Algorithm */
91+
class Edge {
92+
int to;
93+
int weight;
14994

150-
/* Floyd Warshall */
95+
public Edge(int to, int weight){
96+
this.to = to;
97+
this.weight = weight;
98+
}
99+
}
100+
101+
/* Dijkstra Algorithm */
151102
class Solution {
152103
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
153-
// This needs to be a float because it needs to store the Integer.MAX_VALUE.
154-
// Else if this is int, adding a positive number to the max value an integer
155-
// can handle, the bits will overflow and becomes a negative number.
156-
// Alternatively, instead of the MAX_VALUE as a placeholder, since the
157-
// constraint for distanceThreshold <= 10^4, we can initialize it with
158-
// anything greater than the threshold value (i.e., 10001).
159-
float[][] dp = new float[n][n];
160-
161-
// Initialize dp
162-
for (int i = 0; i < n; i++) {
163-
Arrays.fill(dp[i], Integer.MAX_VALUE);
164-
dp[i][i] = 0;
104+
// Create Linked list of edges as the vertex
105+
LinkedList<Edge>[] graph = new LinkedList[n];
106+
107+
for(int i = 0; i < graph.length; i++){
108+
graph[i] = new LinkedList<>();
165109
}
166110

167-
for (int[] edge : edges) {
168-
// Fill dp with from to edge grid; dp[from][to] = weight
169-
dp[edge[0]][edge[1]] = edge[2];
170-
dp[edge[1]][edge[0]] = edge[2];
111+
// Fill the matrix graph with bidirectional direct Edges
112+
for(int[] edge : edges){
113+
graph[edge[0]].add(newEdge(edge[1], edge[2])); // from
114+
graph[edge[1]].add(newEdge(edge[0], edge[2])); // to
171115
}
172116

173-
// Find all shortest path
174-
for (int detour = 0; detour < n; detour++) {
175-
for (int from = 0; from < n; from++) {
176-
for (int to = 0; to < n; to++) {
177-
// Update edge path if detour city is shorter than direct
178-
if (dp[from][to] > dp[from][detour] + dp[detour][to])
179-
dp[from][to] = dp[from][detour] + dp[detour][to];
180-
}
117+
int maxNodes = n + 1;
118+
int maxVertex = -1;
119+
for(int i = 0; i < n; i++){
120+
int visits = bfs(graph, i, distanceThreshold);
121+
if(visits <= maxNodes){
122+
maxVertex = i;
123+
maxNodes = Math.min(maxNodes, visits);
181124
}
182125
}
183126

184-
int maxVisits = n + 1;
185-
int cityWithLesserNeighbors = -1;
186-
for(int from = 0; from < n; from++) {
187-
// Get all neighboring cities with less than distanceThreshold edge
188-
int neighborCitiesWithinLimit = 0;
189-
for(int to = 0; to < n; to++) {
190-
if(dp[from][to] <= distanceThreshold)
191-
neighborCitiesWithinLimit++;
192-
}
193-
if(neighborCitiesWithinLimit <= maxVisits){
194-
cityWithLesserNeighbors = from;
195-
maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit);
127+
return maxVertex;
128+
}
129+
130+
// Breadth-first Search (BFS)
131+
// Returns the number of visited nodes
132+
public int bfs(LinkedList<Edge>[] graph, int vertex, int thresh){
133+
// Storage for the explored vertices
134+
Map<Integer,Integer> map = new HashMap<>();
135+
136+
// (Edge a, Edge b) -> (a.weight - b.weight) is a comparator lambda for
137+
// sorting the smallest value (a.weight - b.weight) first. Therefore, this
138+
// PQ prioritizes smaller numbers first (ascending).
139+
PriorityQueue<Edge> pq = new PriorityQueue<>((Edge a, Edge b) -> (a.weight - b.weight));
140+
// Initialize with new Edge with 0 weight
141+
pq.offer(new Edge(vertex, 0));
142+
143+
while(!pq.isEmpty()){
144+
Edge edge = pq.remove();
145+
146+
// Skip if edge already in the map and weight is greater
147+
if(map.containsKey(edge.to) && edge.weight > map.get(edge.to))
148+
continue;
149+
150+
// Add or update edge to map
151+
map.put(edge.to, edge.weight);
152+
153+
// Traverse next edge
154+
for(Edge e : graph[edge.to]) {
155+
int dist = e.weight + edge.weight;
156+
157+
if(dist > thresh)
158+
continue;
159+
160+
// Skip if edge already in the map and distance is greater
161+
if(map.containsKey(e.to) && dist > map.get(e.to))
162+
continue;
163+
164+
// Add or update edge to map
165+
map.put(e.to, dist);
166+
pq.offer(new Edge(e.to,dist));
196167
}
197168
}
198169

199-
return cityWithLesserNeighbors;
170+
return map.size() - 1;
200171
}
201172
}
173+
174+
// /* Floyd Warshall */
175+
// class Solution {
176+
// public int findTheCity(int n, int[][] edges, int distanceThreshold) {
177+
// // This needs to be a float because it needs to store the Integer.MAX_VALUE.
178+
// // Else if this is int, adding a positive number to the max value an integer
179+
// // can handle, the bits will overflow and becomes a negative number.
180+
// // Alternatively, instead of the MAX_VALUE as a placeholder, since the
181+
// // constraint for distanceThreshold <= 10^4, we can initialize it with
182+
// // anything greater than the threshold value (i.e., 10001).
183+
// float[][] dp = new float[n][n];
184+
//
185+
// // Initialize dp
186+
// for (int i = 0; i < n; i++) {
187+
// Arrays.fill(dp[i], Integer.MAX_VALUE);
188+
// dp[i][i] = 0;
189+
// }
190+
//
191+
// for (int[] edge : edges) {
192+
// // Fill dp with from to edge grid; dp[from][to] = weight
193+
// dp[edge[0]][edge[1]] = edge[2];
194+
// dp[edge[1]][edge[0]] = edge[2];
195+
// }
196+
//
197+
// // Find all shortest path
198+
// for (int detour = 0; detour < n; detour++) {
199+
// for (int from = 0; from < n; from++) {
200+
// for (int to = 0; to < n; to++) {
201+
// // 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];
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 によって変換されたページ (->オリジナル) /