Switchboard rings: Positive proof strip
Stuck or curious how this works? Visualize Breadth-First Search (BFS) ↗
Inputs
links is a square table of 0s and 1s. Points are numbered from 0. A 1 at links[a][b] means a one-way path goes from point a to point b. A 0 means no path.
Your tasks
- Create a list
distances. A graph is a set of points joined by paths. Here, paths go in one direction. Start at point 0. For each point, find the fewest paths you must follow to reach it. Record these distances in point order: 0, 1, 2, and so on. The distance to point 0 is 0. Record -1 for a point you cannot reach. A graph with no points gives an emptydistances. - Create a list
positiveValuesthat keeps only the numbers indistancesthat are greater than 0, in the same order. - Complete
switchboardRingsPositiveProofStripso it returnspositiveValues. Return it, do not print it.
Example
If links is [[0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1], [1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 1, 1], [1, 0, 0, 1, 0, 1]], then distances is [0, 1, 2, 1, 1, 2] and switchboardRingsPositiveProofStrip returns [1, 2, 1, 1, 2].
Rules
- Keep repeated numbers.
- If no number is greater than 0, return [].
Test data
TEST DATA 1: links is [[0, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0], [0, 1, 0, 1, 1, 0]].
TEST DATA 2: links is [[0, 0, 0, 1, 0], [1, 0, 0, 0, 0], [0, 0, 1, 0, 1], [0, 1, 0, 1, 1], [1, 0, 1, 1, 1]].
Examples
Input: links = [[0,1,1,0,0,0],[0,1,1,1,0,0],[0,0,1,1,0,1],[0,0,0,0,0,1],[0,0,0,1,0,0],[0,1,0,1,1,0]] Output: [1,1,2,3,2]
distances is [0, 1, 1, 2, 3, 2]. Keep only numbers greater than 0, without changing their order. Return [1, 1, 2, 3, 2].
Input: links = [[0,0,0,1,0],[1,0,0,0,0],[0,0,1,0,1],[0,1,0,1,1],[1,0,1,1,1]] Output: [2,3,1,2]
distances is [0, 2, 3, 1, 2]. Keep only numbers greater than 0, without changing their order. Return [2, 3, 1, 2].
Input: links = [] Output: []
distances is []. Keep only numbers greater than 0, without changing their order. Return [].
Input limits
- 0 <= input size <= 16.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
- The graph has at most 16 points. Its table has the same number of rows and columns. Link entries are 0 or 1; cost entries are 0 through 13.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(V^2); extra memory, including the helper list, O(n). n is the input length; V is the number of graph points. These labels describe how work and memory grow with the input.
Practice focus
Build distances first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.