Kruskal's MST Algorithm
JavaScript
hard
45 mins
Given an integer n representing the number of nodes (0 to n-1) and an array of edges where each edge is [u, v, weight], return the sum of weights of the edges in the Minimum Spanning Tree.
Examples
Input:
n = 4
edges = [
[0, 1, 4],
[0, 2, 1],
[1, 2, 2],
[1, 3, 5],
[2, 3, 3]
]
Output: 6
Input:
n = 3
edges = [
[0, 1, 1],
[1, 2, 2],
[0, 2, 5]
]
Output: 3
Input:
n = 1
edges = []
Output: 0
Constraints
n<= 1000edges.length<= 10000- Weights are non-negative integers.
- The graph is connected.
Companies:
Atlassian
amazon
microsoft
apple
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
