Skip to content

Commit 6e29f03

Browse files
committed
1 parent bcbbe8d commit 6e29f03

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,3 +1327,4 @@ mod s1755_closest_subsequence_sum;
13271327
mod s1758_minimum_changes_to_make_alternating_binary_string;
13281328
mod s1759_count_number_of_homogenous_substrings;
13291329
mod s1760_minimum_limit_of_balls_in_a_bag;
1330+
mod s1761_minimum_degree_of_a_connected_trio_in_a_graph;
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* [1761] Minimum Degree of a Connected Trio in a Graph
3+
*
4+
* You are given an undirected graph. You are given an integer n which is the number of nodes in the graph and an array edges, where each edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi.
5+
* A connected trio is a set of three nodes where there is an edge between every pair of them.
6+
* The degree of a connected trio is the number of edges where one endpoint is in the trio, and the other is not.
7+
* Return the minimum degree of a connected trio in the graph, or -1 if the graph has no connected trios.
8+
*
9+
* Example 1:
10+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios1.png" style="width: 388px; height: 164px;" />
11+
* Input: n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
12+
* Output: 3
13+
* Explanation: There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above.
14+
*
15+
* Example 2:
16+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/01/26/trios2.png" style="width: 388px; height: 164px;" />
17+
* Input: n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]
18+
* Output: 0
19+
* Explanation: There are exactly three trios:
20+
* 1) [1,4,3] with degree 0.
21+
* 2) [2,5,6] with degree 2.
22+
* 3) [5,6,7] with degree 2.
23+
*
24+
*
25+
* Constraints:
26+
*
27+
* 2 <= n <= 400
28+
* edges[i].length == 2
29+
* 1 <= edges.length <= n * (n-1) / 2
30+
* 1 <= ui, vi <= n
31+
* ui != vi
32+
* There are no repeated edges.
33+
*
34+
*/
35+
pub struct Solution {}
36+
37+
// problem: https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/
38+
// discuss: https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/discuss/?currentPage=1&orderBy=most_votes&query=
39+
40+
// submission codes start here
41+
42+
impl Solution {
43+
// Credit: https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/solutions/3209233/just-a-runnable-solution/
44+
pub fn min_trio_degree(n: i32, edges: Vec<Vec<i32>>) -> i32 {
45+
let n = n as usize;
46+
let mut al = vec![vec![]; n + 1];
47+
let mut cnt = vec![0; n + 1];
48+
let mut result = std::i32::MAX;
49+
50+
for e in edges.iter() {
51+
let t1 = e[0].min(e[1]) as usize;
52+
let t2 = e[0].max(e[1]) as usize;
53+
al[t1].push(t2);
54+
cnt[t1] += 1;
55+
cnt[t2] += 1;
56+
}
57+
58+
for t1 in 1..=n {
59+
for &t2 in al[t1].iter() {
60+
for &t3 in al[t1].iter() {
61+
if t2 < t3 && al[t2].contains(&t3) {
62+
result = result.min(cnt[t1] + cnt[t2] + cnt[t3] - 6);
63+
}
64+
}
65+
}
66+
}
67+
68+
if result == std::i32::MAX {
69+
-1
70+
} else {
71+
result
72+
}
73+
}
74+
}
75+
76+
// submission codes end
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::*;
81+
82+
#[test]
83+
fn test_1761_example_1() {
84+
let n = 6;
85+
let edges = vec![
86+
vec![1, 2],
87+
vec![1, 3],
88+
vec![3, 2],
89+
vec![4, 1],
90+
vec![5, 2],
91+
vec![3, 6],
92+
];
93+
94+
let result = 3;
95+
96+
assert_eq!(Solution::min_trio_degree(n, edges), result);
97+
}
98+
99+
#[test]
100+
fn test_1761_example_2() {
101+
let n = 7;
102+
let edges = vec![
103+
vec![1, 3],
104+
vec![4, 1],
105+
vec![4, 3],
106+
vec![2, 5],
107+
vec![5, 6],
108+
vec![6, 7],
109+
vec![7, 5],
110+
vec![2, 6],
111+
];
112+
113+
let result = 0;
114+
115+
assert_eq!(Solution::min_trio_degree(n, edges), result);
116+
}
117+
}

0 commit comments

Comments
 (0)