Skip to the code
cf.
+25 XP

#467 Warehouse With the Fewest Nearby Neighbors

medium

A logistics dashboard has n warehouses numbered 0 to n - 1, joined by two-way roads roads[i] = [a, b, km]. Two warehouses are neighbors if the shortest road distance between them is at most limit km.

Write quietestWarehouse(n, roads, limit) that returns the warehouse with the fewest neighbors. If several warehouses tie, return the smallest number.

Example 1

Input: n = 4, roads = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], limit = 4
Output: 0

Warehouse 0 reaches 1 (3 km) and 2 (4 km) but not 3 (5 km): 2 neighbors. Warehouse 3 also has 2, while 1 and 2 have 3. Of 0 and 3, the smaller number is 0.

Example 2

Input: n = 2, roads = [], limit = 10
Output: 0

Neither warehouse has a neighbor, so they tie with 0 and the answer is the smaller number.

Constraints

  • 2 <= n <= 100
  • 0 <= roads.length <= n * (n - 1) / 2
  • 1 <= km, limit <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).