Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 1466 · Graph Fundamentals / DFS

Reorder Routes to Make All Paths Lead to the City Zero

LeetCode 1466 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

DFS from 0 on the undirected graph; count edges pointing away.

मध्यमDepth-First SearchBreadth-First SearchGraph Theory

मी Reorder Routes to Make All Paths Lead to the City Zero (LeetCode 1466) असा सोडवला — Graph Fundamentals / DFS पॅटर्नमधील एक मध्यम प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

खालील विवरण इंग्रजीत आहे.

Intuition

Walk outward from city 0 over the roads ignoring direction. Every road you traverse in its original direction points away from 0, so it must be reversed; roads you traverse backwards already point toward 0.

Approach

  1. 1

    Undirected adjacency plus a set of original directed edges.

  2. 2

    DFS from 0.

  3. 3

    Moving node → neighbour along an original edge (node, neighbour) means it needs flipping.

  4. 4

    Return the count.

मी कसा सोडवला

DFS with edge directionsPythonO(n) time
class Solution:
    def minReorder(self, n: int, connections: list[list[int]]) -> int:
        """
        Intuition is if we start from zero/destination and go from destination that's not what we wanted. So that we need to reverse and count the number of reverses. We can maintain an undirected graph and directed graph set, will traverse from zero, if we can traverse from zero and that edge exists in directed graph set, we can think of it to reverse
        """
        adj_list=defaultdict(list)
        directed_edges = set()
        for u,v in connections:
            adj_list[u].append(v)
            adj_list[v].append(u)
            directed_edges.add((u,v))
        visited = set()
        count=0
        def dfs(node):
            nonlocal count
            visited.add(node)
            for adj_node in adj_list[node]:
                if adj_node not in visited:
                    if (node,adj_node) in directed_edges:
                        count+=1
                    dfs(adj_node)
        dfs(0)
        return count
स्टेप-थ्रू लोड होत आहे…

Reorder Routes to Make All Paths Lead to the City Zero — वारंवार विचारले जाणारे प्रश्न

Reorder Routes to Make All Paths Lead to the City Zero मागील intuition काय आहे?

Walk outward from city 0 over the roads ignoring direction. Every road you traverse in its original direction points away from 0, so it must be reversed; roads you traverse backwards already point toward 0.

Reorder Routes to Make All Paths Lead to the City Zero (LeetCode 1466) कसा सोडवावा?

1. Undirected adjacency plus a set of original directed edges. 2. DFS from 0. 3. Moving node → neighbour along an original edge (node, neighbour) means it needs flipping. 4. Return the count.

Reorder Routes to Make All Paths Lead to the City Zero ची time आणि space complexity किती आहे?

Time: O(n). Space: O(n). (Accepted approach: DFS with edge directions.)

Reorder Routes to Make All Paths Lead to the City Zero मध्ये सामान्य चुका कोणत्या?

Count edges by their original direction, not the direction you travel.