|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/armstrong-number "Armstrong Number") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/parallel-courses "Parallel Courses") |
| 11 | + |
| 12 | +## 1135. Connecting Cities With Minimum Cost (Medium) |
| 13 | + |
| 14 | +<p>There are <code>N</code> cities numbered from 1 to <code>N</code>.</p> |
| 15 | + |
| 16 | +<p>You are given <code>connections</code>, where each <code>connections[i] = [city1, city2, cost]</code> represents the cost to connect <code>city1</code> and <code>city2</code> together. (A <em>connection</em> is bidirectional: connecting <code>city1</code> and <code>city2</code> is the same as connecting <code>city2</code> and <code>city1</code>.)</p> |
| 17 | + |
| 18 | +<p>Return the minimum cost so that for every pair of cities, there exists a path of connections (possibly of length 1) that connects those two cities together. The cost is the sum of the connection costs used. If the task is impossible, return -1.</p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | + |
| 22 | +<p><strong>Example 1:</strong></p> |
| 23 | + |
| 24 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/04/20/1314_ex2.png" style="width: 161px; height: 141px;" /></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input: </strong>N = 3, connections = [[1,2,5],[1,3,6],[2,3,1]] |
| 28 | +<strong>Output: </strong>6 |
| 29 | +<strong>Explanation: </strong> |
| 30 | +Choosing any 2 edges will connect all cities so we choose the minimum 2. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong>Example 2:</strong></p> |
| 34 | + |
| 35 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/04/20/1314_ex1.png" style="width: 136px; height: 91px;" /></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input: </strong>N = 4, connections = [[1,2,3],[3,4,4]] |
| 39 | +<strong>Output: </strong>-1 |
| 40 | +<strong>Explanation: </strong> |
| 41 | +There is no way to connect all cities even if all edges are used. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | + |
| 46 | +<p><strong>Note:</strong></p> |
| 47 | + |
| 48 | +<ol> |
| 49 | + <li><code>1 <= N <= 10000</code></li> |
| 50 | + <li><code>1 <= connections.length <= 10000</code></li> |
| 51 | + <li><code>1 <= connections[i][0], connections[i][1] <= N</code></li> |
| 52 | + <li><code>0 <= connections[i][2] <= 10^5</code></li> |
| 53 | + <li><code>connections[i][0] != connections[i][1]</code></li> |
| 54 | +</ol> |
| 55 | + |
| 56 | +### Related Topics |
| 57 | + [[Union Find](https://github.com/openset/leetcode/tree/master/tag/union-find/README.md)] |
| 58 | + [[Graph](https://github.com/openset/leetcode/tree/master/tag/graph/README.md)] |
| 59 | + |
| 60 | +### Hints |
| 61 | +<details> |
| 62 | +<summary>Hint 1</summary> |
| 63 | +What if we model the cities as a graph? |
| 64 | +</details> |
| 65 | + |
| 66 | +<details> |
| 67 | +<summary>Hint 2</summary> |
| 68 | +Build a graph of cities and find the minimum spanning tree. |
| 69 | +</details> |
| 70 | + |
| 71 | +<details> |
| 72 | +<summary>Hint 3</summary> |
| 73 | +You can use a variation of the Kruskal's algorithm for that. |
| 74 | +</details> |
| 75 | + |
| 76 | +<details> |
| 77 | +<summary>Hint 4</summary> |
| 78 | +Sort the edges by their cost and use a union-find data structure. |
| 79 | +</details> |
| 80 | + |
| 81 | +<details> |
| 82 | +<summary>Hint 5</summary> |
| 83 | +How to check all cities are connected? |
| 84 | +</details> |
| 85 | + |
| 86 | +<details> |
| 87 | +<summary>Hint 6</summary> |
| 88 | +At the beginning we have n connected components, each time we connect two components the number of connected components is reduced by one. At the end we should end with only a single component otherwise return -1. |
| 89 | +</details> |
0 commit comments