Weighted lines: Proof strip
Stuck or curious how this works? Visualize Binary Heap Operations ↗
Inputs
links is a square table of travel costs. Points are numbered from 0. A positive number at links[a][b] is the cost to go from point a to point b. A 0 means no path, not a free path.
Your tasks
- Create a list
costs. Each point in this graph is a place, and each one-way path has a travel cost. Start at point 0. Find the lowest total travel cost to each point. Put these costs in point order. The cost to point 0 is 0. Record -1 for any point you cannot reach. A graph with no points gives an emptycosts. - Complete
weightedLinesProofStripso it returnscosts. Return it, do not print it.
Example
If links is [[0, 13, 0, 11, 9, 0], [0, 0, 0, 0, 7, 0], [0, 0, 0, 1, 6, 0], [7, 7, 13, 0, 0, 0], [0, 10, 0, 13, 0, 0], [2, 0, 1, 3, 7, 13]], then costs is [0, 13, 24, 11, 9, -1] and weightedLinesProofStrip returns [0, 13, 24, 11, 9, -1].
Rules
- If
costsis empty, return [].
Test data
TEST DATA 1: links is [[0, 13, 0, 2, 4, 0], [7, 8, 4, 8, 9, 0], [0, 0, 6, 0, 7, 8], [0, 0, 3, 0, 2, 10], [0, 10, 0, 0, 0, 8], [6, 0, 0, 0, 0, 0]].
TEST DATA 2: links is [[0, 0, 0, 0, 0], [5, 12, 11, 6, 9], [0, 7, 0, 0, 0], [0, 0, 5, 0, 0], [0, 4, 4, 0, 2]].
Examples
Input: links = [[0,13,0,2,4,0],[7,8,4,8,9,0],[0,0,6,0,7,8],[0,0,3,0,2,10],[0,10,0,0,0,8],[6,0,0,0,0,0]] Output: [0,13,5,2,4,12]
costs is [0, 13, 5, 2, 4, 12]. weightedLinesProofStrip returns that whole list, so return [0, 13, 5, 2, 4, 12].
Input: links = [[0,0,0,0,0],[5,12,11,6,9],[0,7,0,0,0],[0,0,5,0,0],[0,4,4,0,2]] Output: [0,-1,-1,-1,-1]
costs is [0, -1, -1, -1, -1]. weightedLinesProofStrip returns that whole list, so return [0, -1, -1, -1, -1].
Input: links = [] Output: []
costs is []. weightedLinesProofStrip returns that whole list, so 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 + E log V); extra memory, including the helper list, O(n). n is the input length; V is the number of graph points; E is the number of graph links. These labels describe how work and memory grow with the input.
Practice focus
Build costs 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.