Skip to content

Commit 5bbd9da

Browse files
Solve question and add code
1 parent 3e196a5 commit 5bbd9da

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Intuition
2+
When given a graph with cities and distances between them, we want to find the city that has the fewest number of reachable cities within a given distance threshold. This requires understanding the shortest paths between cities and then evaluating each city's reachability within the threshold.
3+
4+
Approach
5+
Initialization:
6+
Use a 2D matrix dist to represent the shortest distances between every pair of cities. Initialize the distances with INT_MAX (infinity), except the diagonal (self-distances) which are set to 0.
7+
Set Initial Distances:
8+
Populate the dist matrix with the given edge weights. For each edge [u, v, w], set dist[u][v] and dist[v][u] to w.
9+
Compute Shortest Paths:
10+
Use the Floyd-Warshall algorithm to compute the shortest paths between all pairs of cities. This algorithm iteratively updates the dist matrix to ensure that each entry dist[i][j] holds the shortest distance from city i (source) to city j (Destination).
11+
Count Reachable Cities:
12+
For each city, count how many other cities are reachable within the specified distance threshold.
13+
Determine the Result:
14+
Find the city with the smallest count of reachable cities. If there are multiple cities with the same count, choose the city with the highest index.
15+
Complexity
16+
Time complexity: O(n^3), since we use Floyd-Washall algorithm O(n^3) and also then iterate through whole matrix O(n^2) -> O(n^3)
17+
Space complexity: The primary space usage comes from the 'dist' matrix, which requires O(n^2) space to store distances between all pairs of cities. Thus, the space complexity is O(n^3).

208-26th_July-Find_city/code.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
class Solution
2+
{
3+
public:
4+
int findTheCity(int n, vector<vector<int>> &edges, int distanceThreshold)
5+
{
6+
vector<vector<int>> dist(n, vector<int>(n, INT_MAX));
7+
for (int i = 0; i < n; i++)
8+
{
9+
dist[i][i] = 0;
10+
}
11+
12+
for (auto &edge : edges)
13+
{
14+
int u = edge[0];
15+
int v = edge[1];
16+
int w = edge[2];
17+
dist[u][v] = w;
18+
dist[v][u] = w;
19+
}
20+
21+
// for (auto& row : dist) {
22+
// for (auto& elem : row) {
23+
// if (elem == INT_MAX) {
24+
// cout << "INF ";
25+
// } else {
26+
// cout << elem << " ";
27+
// }
28+
// }
29+
// cout << endl;
30+
// }
31+
32+
for (int k = 0; k < n; k++)
33+
{
34+
for (int i = 0; i < n; i++)
35+
{
36+
for (int j = 0; j < n; j++)
37+
{
38+
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX)
39+
{
40+
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
41+
}
42+
}
43+
}
44+
}
45+
// cout<<"Updated array::"<<endl;
46+
// for (auto& row : dist) {
47+
// for (auto& elem : row) {
48+
// if (elem == INT_MAX) {
49+
// cout << "INF ";
50+
// } else {
51+
// cout << elem << " ";
52+
// }
53+
// }
54+
// cout << endl;
55+
// }
56+
57+
int res = -1;
58+
int mini = INT_MAX;
59+
60+
for (int i = 0; i < n; i++)
61+
{
62+
int city = 0;
63+
for (int j = 0; j < n; j++)
64+
{
65+
if (i != j && dist[i][j] <= distanceThreshold)
66+
{
67+
city++;
68+
}
69+
}
70+
71+
if (city <= mini)
72+
{
73+
mini = city;
74+
res = i;
75+
}
76+
}
77+
return res;
78+
}
79+
};

0 commit comments

Comments
 (0)