A campus has n buildings numbered 0 to n - 1. Each options[i] = [a, b, cost] is a possible fiber link between buildings a and b and what it would cost. Links work both ways.
Write cheapestNetwork(n, options) that returns the smallest total cost of links that connects every building, or -1 if it's impossible.
Input: n = 4, options = [[0,1,1],[1,2,2],[0,2,4],[2,3,3],[1,3,5]] Output: 6
Links 0-1, 1-2, and 2-3 cost 1 + 2 + 3 = 6 and connect everything.
Input: n = 3, options = [[0,1,5]] Output: -1
Building 2 has no possible link.
1 <= n <= 100000 <= options.length <= 500001 <= cost <= 10^5Run 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 #471 Connect the Office Buildings. 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.