LeetCode 题解工作台
一个图中连通三元组的最小度数
给你一个无向图,整数 n 表示图中节点的数目, edges 数组表示图中的边,其中 edges[i] = [u i , v i ] ,表示 u i 和 v i 之间有一条无向边。 一个 连通三元组 指的是 三个 节点组成的集合且这三个点之间 两两 有边。 连通三元组的度数 是所有满足此条件的边的数目…
2
题型
5
代码语言
3
相关题
当前训练重点
困难 · 图
答案摘要
我们先将所有边存入邻接矩阵 中,再将每个节点的度数存入数组 中。初始化答案 。 然后枚举所有的三元组 $(i, j, k)$,其中 $i \lt j \lt k$,如果 $\textit{g}[i][j] = \textit{g}[j][k] = \textit{g}[i][k] = 1$,则说明这三个节点构成了一个连通三元组,此时更新答案为 $\textit{ans} = \min(\tex…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 图 题型思路
题目描述
给你一个无向图,整数 n 表示图中节点的数目,edges 数组表示图中的边,其中 edges[i] = [ui, vi] ,表示 ui 和 vi 之间有一条无向边。
一个 连通三元组 指的是 三个 节点组成的集合且这三个点之间 两两 有边。
连通三元组的度数 是所有满足此条件的边的数目:一个顶点在这个三元组内,而另一个顶点不在这个三元组内。
请你返回所有连通三元组中度数的 最小值 ,如果图中没有连通三元组,那么返回 -1 。
示例 1:
输入:n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]] 输出:3 解释:只有一个三元组 [1,2,3] 。构成度数的边在上图中已被加粗。
示例 2:
输入:n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]] 输出:0 解释:有 3 个三元组: 1) [1,4,3],度数为 0 。 2) [2,5,6],度数为 2 。 3) [5,6,7],度数为 2 。
提示:
2 <= n <= 400edges[i].length == 21 <= edges.length <= n * (n-1) / 21 <= ui, vi <= nui != vi- 图中没有重复的边。
解题思路
方法一:暴力枚举
我们先将所有边存入邻接矩阵 中,再将每个节点的度数存入数组 中。初始化答案 。
然后枚举所有的三元组 ,其中 ,如果 ,则说明这三个节点构成了一个连通三元组,此时更新答案为 。
枚举完所有的三元组后,如果答案仍然为 ,说明图中不存在连通三元组,返回 ,否则返回答案。
时间复杂度 ,空间复杂度 。其中 为节点数。
def min(a: int, b: int) -> int:
return a if a < b else b
class Solution:
def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
g = [[False] * n for _ in range(n)]
deg = [0] * n
for u, v in edges:
u, v = u - 1, v - 1
g[u][v] = g[v][u] = True
deg[u] += 1
deg[v] += 1
ans = inf
for i in range(n):
for j in range(i + 1, n):
if g[i][j]:
for k in range(j + 1, n):
if g[i][k] and g[j][k]:
ans = min(ans, deg[i] + deg[j] + deg[k] - 6)
return -1 if ans == inf else ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Understanding of graph enumeration patterns and efficient counting techniques.
- question_mark
Ability to apply traversal strategies for reducing unnecessary computations.
- question_mark
Comfort with optimizing brute force solutions to scale to larger inputs.
常见陷阱
外企场景- error
Incorrectly counting the degree of a trio by missing some edges between nodes.
- error
Failing to properly check all connected trios, especially in sparse graphs.
- error
Using brute force methods without optimization that could lead to excessive runtime for larger graphs.
进阶变体
外企场景- arrow_right_alt
Find the maximum degree of a connected trio.
- arrow_right_alt
Handle directed graphs where the degree of a trio might change based on edge direction.
- arrow_right_alt
Modify the problem to allow for weighted edges and find the minimum weighted degree of a connected trio.