Code RoomMinimum spanning tree
MediumPrep Room Coding #67

Minimum spanning tree

CodingAlgorithms & data structuresMid–Senior~35 min

You must connect n cities (0..n-1) with cables. You are given a list of candidate cables as [u, v, cost]. Return the minimum total cost to connect all cities so that every city is reachable from every other. If it is impossible to connect them all, return -1. The graph is undirected; duplicate cables and self-cables (u == v) may appear.

Implement
min_cable_cost(n: int, cables: list[list[int]]) → int
Examples
in[4,[[0,1,1],[1,2,2],[2,3,3],[0,3,10]]]out6
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[4,[[0,1,1],[1,2,2],[2,3,3],[0,3,10]]]6not run yetsample