Facebook Pixel

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 <= 1000
  • edges.length <= 10000
  • Weights are non-negative integers.
  • The graph is connected.

Companies:

Atlassian
amazon
microsoft
apple

Solve Similar questions šŸ”„

Please Login.
Please Login.