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
LeetCode లో తెరవండి ↗

నేను 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.