|
| 1 | +# [1761. Minimum Degree of a Connected Trio in a Graph](https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You are given an undirected graph. You are given an integer <code>n</code> which is the number of nodes in the graph and an array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an undirected edge between <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.</p> |
| 6 | + |
| 7 | +<p>A <strong>connected trio</strong> is a set of <strong>three</strong> nodes where there is an edge between <b>every</b> pair of them.</p> |
| 8 | + |
| 9 | +<p>The <strong>degree of a connected trio</strong> is the number of edges where one endpoint is in the trio, and the other is not.</p> |
| 10 | + |
| 11 | +<p>Return <em>the <strong>minimum</strong> degree of a connected trio in the graph, or</em> <code>-1</code> <em>if the graph has no connected trios.</em></p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios1.png" style="width: 388px; height: 164px;"> |
| 16 | +<pre><strong>Input:</strong> n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]] |
| 17 | +<strong>Output:</strong> 3 |
| 18 | +<strong>Explanation:</strong> There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above. |
| 19 | +</pre> |
| 20 | + |
| 21 | +<p><strong class="example">Example 2:</strong></p> |
| 22 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios2.png" style="width: 388px; height: 164px;"> |
| 23 | +<pre><strong>Input:</strong> n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]] |
| 24 | +<strong>Output:</strong> 0 |
| 25 | +<strong>Explanation:</strong> There are exactly three trios: |
| 26 | +1) [1,4,3] with degree 0. |
| 27 | +2) [2,5,6] with degree 2. |
| 28 | +3) [5,6,7] with degree 2. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Constraints:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li><code>2 <= n <= 400</code></li> |
| 36 | + <li><code>edges[i].length == 2</code></li> |
| 37 | + <li><code>1 <= edges.length <= n * (n-1) / 2</code></li> |
| 38 | + <li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li> |
| 39 | + <li><code>u<sub>i </sub>!= v<sub>i</sub></code></li> |
| 40 | + <li>There are no repeated edges.</li> |
| 41 | +</ul> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | + |
| 46 | +## Solutions |
| 47 | + |
| 48 | +**Solution: `Brute Force`** |
| 49 | + |
| 50 | +- Time complexity: <em>O(n<sup>3</sup>)</em> |
| 51 | +- Space complexity: <em>O(n<sup>2</sup>)</em> |
| 52 | + |
| 53 | +<p> </p> |
| 54 | + |
| 55 | +### **JavaScript** |
| 56 | + |
| 57 | +```js |
| 58 | +/** |
| 59 | + * @param {number} n |
| 60 | + * @param {number[][]} edges |
| 61 | + * @return {number} |
| 62 | + */ |
| 63 | +const minTrioDegree = function (n, edges) { |
| 64 | + const indegree = Array.from({ length: n + 1 }, () => 0); |
| 65 | + const connected = Array.from({ length: n + 1 }, () => new Array(n + 1).fill(false)); |
| 66 | + let result = Number.MAX_SAFE_INTEGER; |
| 67 | + |
| 68 | + for (const [u, v] of edges) { |
| 69 | + indegree[u] += 1; |
| 70 | + indegree[v] += 1; |
| 71 | + connected[u][v] = true; |
| 72 | + connected[v][u] = true; |
| 73 | + } |
| 74 | + |
| 75 | + for (let u = 1; u <= n - 2; u++) { |
| 76 | + for (let v = u + 1; v <= n - 1; v++) { |
| 77 | + if (!connected[u][v]) continue; |
| 78 | + |
| 79 | + for (let k = v + 1; k <= n; k++) { |
| 80 | + if (!connected[v][k] || !connected[u][k]) continue; |
| 81 | + const degree = indegree[u] + indegree[v] + indegree[k] - 6; |
| 82 | + |
| 83 | + result = Math.min(degree, result); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return result === Number.MAX_SAFE_INTEGER ? -1 : result; |
| 89 | +}; |
| 90 | +``` |
0 commit comments