Skip to content

Commit c0f2262

Browse files
committed
feat: add solution 1761. Minimum Degree of a Connected Trio in a Graph
1 parent 6871350 commit c0f2262

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

solutions/1061. Lexicographically Smallest Equivalent String/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld"
6464
**Solution: `Union Find`**
6565

6666
- Time complexity: <em>O(m+n)</em>
67-
- Space complexity: <em>O(26m -> m)</em>
67+
- Space complexity: <em>O(26+m -> m)</em>
6868

6969
<p>&nbsp;</p>
7070

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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>&nbsp;</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>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>2 &lt;= n &lt;= 400</code></li>
36+
<li><code>edges[i].length == 2</code></li>
37+
<li><code>1 &lt;= edges.length &lt;= n * (n-1) / 2</code></li>
38+
<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= 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>&nbsp;</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>&nbsp;</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+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @return {number}
5+
*/
6+
const minTrioDegree = function (n, edges) {
7+
const indegree = Array.from({ length: n + 1 }, () => 0);
8+
const connected = Array.from({ length: n + 1 }, () => new Array(n + 1).fill(false));
9+
let result = Number.MAX_SAFE_INTEGER;
10+
11+
for (const [u, v] of edges) {
12+
indegree[u] += 1;
13+
indegree[v] += 1;
14+
connected[u][v] = true;
15+
connected[v][u] = true;
16+
}
17+
18+
for (let u = 1; u <= n - 2; u++) {
19+
for (let v = u + 1; v <= n - 1; v++) {
20+
if (!connected[u][v]) continue;
21+
22+
for (let k = v + 1; k <= n; k++) {
23+
if (!connected[v][k] || !connected[u][k]) continue;
24+
const degree = indegree[u] + indegree[v] + indegree[k] - 6;
25+
26+
result = Math.min(degree, result);
27+
}
28+
}
29+
}
30+
31+
return result === Number.MAX_SAFE_INTEGER ? -1 : result;
32+
};

0 commit comments

Comments
 (0)