Class Solution
-
- All Implemented Interfaces:
public final class Solution3608 - Minimum Time for K Connected Components.
Medium
You are given an integer
nand an undirected graph withnnodes labeled from 0 ton - 1. This is represented by a 2D arrayedges, where <code>edgesi = u<sub>i</sub>, v<sub>i</sub>, time<sub>i</sub></code> indicates an undirected edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> that can be removed at <code>time<sub>i</sub></code>.You are also given an integer
k.Initially, the graph may be connected or disconnected. Your task is to find the minimum time
tsuch that after removing all edges withtime <= t, the graph contains at leastkconnected components.Return the minimum time
t.A connected component is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.
Example 1:
Input: n = 2, edges = [0,1,3], k = 2
Output: 3
Explanation:
Initially, there is one connected component
{0, 1}.At
time = 1or2, the graph remains unchanged.At
time = 3, edge[0, 1]is removed, resulting ink = 2connected components{0},{1}. Thus, the answer is 3.
Example 2:
Input: n = 3, edges = [0,1,2,1,2,4], k = 3
Output: 4
Explanation:
Initially, there is one connected component
{0, 1, 2}.At
time = 2, edge[0, 1]is removed, resulting in two connected components{0},{1, 2}.At
time = 4, edge[1, 2]is removed, resulting ink = 3connected components{0},{1},{2}. Thus, the answer is 4.
Example 3:
Input: n = 3, edges = [0,2,5], k = 2
Output: 0
Explanation:
Since there are already
k = 2disconnected components{1},{0, 2}, no edge removal is needed. Thus, the answer is 0.
Constraints:
<code>1 <= n <= 10<sup>5</sup></code>
<code>0 <= edges.length <= 10<sup>5</sup></code>
<code>edgesi = u<sub>i</sub>, v<sub>i</sub>, time<sub>i</sub></code>
<code>0 <= u<sub>i</sub>, v<sub>i</sub>< n</code>
<code>u<sub>i</sub> != v<sub>i</sub></code>
<code>1 <= time<sub>i</sub><= 10<sup>9</sup></code>
1 <= k <= nThere are no duplicate edges.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-