Minimum spanning tree
Given n nodes labeled 0..n-1 and a list of weighted undirected edges [u, v, w], return the total weight of a minimum spanning tree. When several edges have equal weight the choice does not affect total weight, but break ties deterministically by (weight, u, v). If the graph is disconnected and no spanning tree exists, return -1. A single node with no edges has MST weight 0.
Implement
mst_weight(n: int, edges: list[list[int]]) → intExamples
in
[4,[[0,1,1],[1,2,2],[2,3,3],[0,3,4]]]out6What 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 25 min
solution.py
InputExpectedGot
[4,[[0,1,1],[1,2,2],[2,3,3],[0,3,4]]]6not run yetsample