Connecting Cities With Minimum Cost

Connecting Cities With Minimum Cost

There are n cities labeled from 1 to n. You are given an array connections where connections[i] = [u, v, cost] represents a bidirectional road between city u and city v with a given cost. Your task is to find the minimum cost to connect all cities such that there is at least one path between any two cities. If it is impossible to connect all cities, return -1. Constraints: 1 <= n <= 10,000 1 <= connections.length <= 10,000 1 <= cost <= 10^5 Each connection [u, v, cost] satisfies 1 <= u, v <= n and u != v.
JavaKruskal's AlgorithmUnion-Find
00
Read
1
InterviewGPT

AI-powered tools to help you succeed in tech interviews — from resume to offer.

Products

  • Interview Solver
  • Question Bank
  • Golden Blogs
  • Intervipedia
  • Application Tools

Company

  • Pricing
  • FAQ
  • About

Legal

  • Privacy Policy
  • Terms of Service

© 2026 InterviewGPT Inc. All rights reserved.

All systems operationalUS-East

Made with ♥ for developers