Package g1301_1400.s1334_find_the_city_with_the_smallest_number_of_neighbors_at_a_threshold_distance
Class Solution
java.lang.Object
g1301_1400.s1334_find_the_city_with_the_smallest_number_of_neighbors_at_a_threshold_distance.Solution
1334 - Find the City With the Smallest Number of Neighbors at a Threshold Distance.<p>Medium</p>
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p>
<p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p>
<p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges’ weights along that path.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/01/16/find_the_city_01.png" alt="" /></p>
<p><strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The figure above describes the graph.</p>
<p>The neighboring cities at a distanceThreshold = 4 for each city are:</p>
<p>City 0 -> [City 1, City 2]</p>
<p>City 1 -> [City 0, City 2, City 3]</p>
<p>City 2 -> [City 0, City 1, City 3]</p>
<p>City 3 -> [City 1, City 2]</p>
<p>Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/01/16/find_the_city_02.png" alt="" /></p>
<p><strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> The figure above describes the graph.</p>
<p>The neighboring cities at a distanceThreshold = 2 for each city are:</p>
<p>City 0 -> [City 1]</p>
<p>City 1 -> [City 0, City 4]</p>
<p>City 2 -> [City 3, City 4]</p>
<p>City 3 -> [City 2, City 4]</p>
<p>City 4 -> [City 1, City 2, City 3]</p>
<p>The city 0 has 1 neighboring city at a distanceThreshold = 2.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 100</code></li>
<li><code>1 <= edges.length <= n * (n - 1) / 2</code></li>
<li><code>edges[i].length == 3</code></li>
<li><code>0 <= from<sub>i</sub> < to<sub>i</sub> < n</code></li>
<li><code>1 <= weight<sub>i</sub>, distanceThreshold <= 10^4</code></li>
<li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
findTheCity
public int findTheCity(int n, int[][] edges, int maxDist)
-