1761. 一个图中连通三元组的最小度数

每日一题 1761. 一个图中连通三元组的最小度数 难度:困难
1761. 一个图中连通三元组的最小度数_第1张图片
只会强行枚举

class Solution:
    def minTrioDegree(self, n: int, edges: List[List[int]]) -> int:
        fromTo = {}
        for edge in edges:
            a = fromTo.get(edge[0], [])
            a.append(edge[1])
            fromTo[edge[0]] = a
            a = fromTo.get(edge[1], [])
            a.append(edge[0])
            fromTo[edge[1]] = a
        res = 100000
        for i in range(1, n + 1):
            a = fromTo.get(i, [])
            for j in a:
                if i > j:
                    continue
                b = fromTo.get(j, [])
                for k in b:
                    if j > k:
                        continue
                    c = fromTo.get(k, [])
                    if i in c:
                        res = min(res, len(a) + len(b) + len(c) - 6)
        
        return res if res != 100000 else -1

你可能感兴趣的:(用Python刷力扣,算法,python)