Skip to interview prep
Apexed

95. Minimum Spanning Tree Weight (Kruskal)

MediumDisjoint sets

An undirected weighted graph has n vertices numbered 0 to n − 1 and edges given as [u, v, w]. Return the total weight of a minimum spanning tree, or -1 if the graph is not connected.

Example 1

Input: n = 4, edges = [[0,1,1],[1,2,2],[0,2,3],[2,3,4]]
Output: 7

Constraints

  • 1 ≤ n ≤ 10⁴
  • 0 ≤ edges.length ≤ 10⁵
  • Weights may be negative

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Minimum Spanning Tree Weight (Kruskal) — coding interview practice — Apexed