Advertisement

Find the City With the Smallest Number of Neighbors at a Threshold Distance - LeetCode 1334 Solution

Find the City With the Smallest Number of Neighbors at a Threshold Distance - Complete Solution Guide

Find the City With the Smallest Number of Neighbors at a Threshold Distance is LeetCode problem 1334, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There are n cities numbered from 0 to n-1 . Given the array edges where edges[i] = [from i , to i , weight i ] represents a bidirectional and weighted edge between cities from i and to i , and given the integer distanceThreshold . Return the city with the smallest number of cities that are reachable through some path and whose distance is at most distanceThreshold , If there are multiple such cities, return the city with the greatest number. Notice that the distance of a path connecting cities i

Detailed Explanation

The problem asks us to find the city that has the fewest neighboring cities reachable within a given distance threshold. We are given a graph represented by edges (city pairs and their distances) and a maximum distance (`distanceThreshold`). We need to find the city for which the number of cities reachable within `distanceThreshold` is the smallest. If multiple cities satisfy this condition, we should return the city with the highest index.

Solution Approach

The provided solution uses the Floyd-Warshall algorithm to find the shortest path between all pairs of cities. Then, for each city, it counts the number of reachable cities within the given distance threshold. Finally, it returns the city with the smallest number of reachable cities (and the highest index in case of ties).

Step-by-Step Algorithm

  1. Step 1: Initialize a distance matrix `dist` where `dist[i][j]` represents the shortest distance between city `i` and city `j`. Initially, set `dist[i][j]` to infinity (or a very large number) if there is no direct edge between `i` and `j`, and to the weight of the edge if there is a direct edge. Set `dist[i][i]` to 0.
  2. Step 2: Populate the `dist` matrix with direct edge weights from the `edges` input.
  3. Step 3: Apply the Floyd-Warshall algorithm: Iterate through all possible intermediate cities `k` from 0 to n-1. For each pair of cities `i` and `j`, update `dist[i][j]` to the minimum of its current value and `dist[i][k] + dist[k][j]`. This step finds the shortest path between all pairs of cities considering all possible intermediate nodes.
  4. Step 4: Iterate through each city `i` from 0 to n-1 and count the number of reachable neighboring cities. A city `j` is reachable from city `i` if `i != j` and `dist[i][j] <= distanceThreshold`.
  5. Step 5: Keep track of the city with the smallest number of reachable neighbors (`min_neighbors`) and its corresponding index (`result_city`). If a city has a smaller or equal number of reachable neighbors than the current minimum, update `min_neighbors` and `result_city`. Note that since we want the largest index in case of a tie, we only update if `reachable_neighbors <= min_neighbors` and the city index is greater than the current `result_city` or the `reachable_neighbors` is strictly less than `min_neighbors`
  6. Step 6: Return the `result_city`.

Key Insights

  • Insight 1: The problem requires finding the shortest paths between all pairs of cities, which suggests using the Floyd-Warshall algorithm.
  • Insight 2: After calculating the shortest path between all pairs of cities, we can iterate through each city and count how many other cities are reachable within the `distanceThreshold`.
  • Insight 3: The city with the minimum number of reachable cities should be tracked. If a city has the same number of reachable cities as the current minimum, we should choose the city with the larger index.

Complexity Analysis

Time Complexity: O(n^3)

Space Complexity: O(n^2)

Topics

This problem involves: Dynamic Programming, Graph, Shortest Path.

Companies

Asked at: Expedia.