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.
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.
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.
2 <= n <= 1000 <= roads.length <= n * (n - 1) / 21 <= km, limit <= 10^4Run 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).
I'm here to help with #467 Warehouse With the Fewest Nearby Neighbors. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.