Weighted lines: Threshold file
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. - Count the numbers in
coststhat are at leastk(greater than or equal tok). Store the count inatLeastK. - Complete
weightedLinesThresholdFileso it returnsatLeastK. Return it, do not print it.
Example
If links is [[3, 0, 9, 0, 13, 10], [1, 0, 0, 3, 0, 0], [13, 5, 3, 0, 0, 13], [0, 0, 0, 0, 7, 0], [7, 0, 1, 5, 0, 0], [0, 13, 1, 0, 12, 0]] and k is 1, then costs is [0, 14, 9, 17, 13, 10] and weightedLinesThresholdFile returns 5.
Rules
- If
costsis empty, the count is 0. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: links is [[0, 6, 0, 0, 11, 4], [0, 6, 12, 0, 8, 0], [3, 9, 13, 0, 9, 0], [6, 0, 0, 0, 0, 0], [0, 2, 0, 0, 10, 10], [0, 0, 0, 8, 12, 5]] and k is 3.
TEST DATA 2: links is [[0, 7, 12, 13, 0], [0, 0, 0, 0, 0], [4, 9, 0, 0, 0], [0, 0, 0, 5, 0], [0, 0, 8, 0, 4]] and k is 2.
Examples
Input: links = [[0,6,0,0,11,4],[0,6,12,0,8,0],[3,9,13,0,9,0],[6,0,0,0,0,0],[0,2,0,0,10,10],[0,0,0,8,12,5]] k = 3 Output: 5
costs is [0, 6, 18, 12, 11, 4]. Here k is 3. The numbers at least 3 are [6, 18, 12, 11, 4]. There are 5, so return 5.
Input: links = [[0,7,12,13,0],[0,0,0,0,0],[4,9,0,0,0],[0,0,0,5,0],[0,0,8,0,4]] k = 2 Output: 3
costs is [0, 7, 12, 13, -1]. Here k is 2. The numbers at least 2 are [7, 12, 13]. There are 3, so return 3.
Input: links = [] k = 2 Output: 0
costs is []. Here k is 2. The numbers at least 2 are []. There are 0, so return 0.
Input limits
- 0 <= input size <= 16.
- k is a whole number from 1 to 8. 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.