Number of good paths

January 15, 2023

tree

Problem URL: Number of good paths

We will try to build the graph from nodes with the smallest value to the largest value. If we build the graph in this way, nodes in the graph will always be smaller than or equal to the value we are checking, then we just need to calculate nCr for each connected graph, explained more blow.

class Solution:
    def numberOfGoodPaths(self, vals: List[int], edges: List[List[int]]) -> int:
        UF = {}

        def find(x):
            UF.setdefault(x,x)
            if x != UF[x]:
                UF[x] = find(UF[x])
            return UF[x]

        def union(x,y):
            UF[find(x)] = find(y)

        tree = collections.defaultdict(list)
        val2Nodes = collections.defaultdict(set)
        for s, e in edges:
            tree[s].append(e)
            tree[e].append(s)
            val2Nodes[vals[s]].add(s)
            val2Nodes[vals[e]].add(e)

        res = len(vals)
        for v in sorted(val2Nodes.keys()):
            for node in val2Nodes[v]:
                for nei in tree[node]:
                    if vals[nei] <= v:
                        union(node, nei)

            groupCount = defaultdict(int)
            for node in val2Nodes[v]:
                groupCount[find(node)] += 1

            for root in groupCount.keys():
                res += math.comb(groupCount[root], 2)

        return res

Time complexity: O(nlog(n))
Space complexity: O(n)