Network Cable Cost - Problem
A company has n offices numbered 0 to n - 1. You can install network cables between offices.
You are given a 2D array cables where cables[i] = [u, v, cost] represents a cable option connecting office u to office v with installation cost cost.
Return the minimum total cost to connect all offices, or -1 if it's impossible.
Input & Output
Example 1
$
Input:
n=4, cables=[[0,1,10],[0,2,6],[0,3,5],[1,3,15],[2,3,4]]
›
Output:
19
💡 Note:
Pick cables (2,3,4)+(0,3,5)+(0,1,10)=19.
Example 2
$
Input:
n=3, cables=[[0,1,1],[1,2,2],[0,2,3]]
›
Output:
3
💡 Note:
Pick (0,1,1)+(1,2,2)=3.
Example 3
$
Input:
n=4, cables=[[0,1,1],[2,3,2]]
›
Output:
-1
💡 Note:
Cannot connect all offices.
Constraints
- 1 ≤ n ≤ 2×10^5
- 0 ≤ cables.length ≤ 2×10^5
- 0 ≤ u, v < n
- 1 ≤ cost ≤ 10^6
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code