Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1976 · Shortest Path / Dijkstra

Number of Ways to Arrive at Destination

LeetCode 1976 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Dijkstra + ways: faster ⇒ copy ways, equal ⇒ add ways (mod 1e9+7).

நடுத்தரம்Dynamic ProgrammingGraph TheoryTopological SortShortest PathDijkstra's Algorithm
LeetCode இல் திற ↗

நான் Number of Ways to Arrive at Destination (LeetCode 1976) ஐ இப்படித் தீர்த்தேன் — Shortest Path / Dijkstra பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Run Dijkstra from 0, but alongside each node's shortest time also keep how many shortest routes reach it. A strictly faster route replaces the count; an equally fast route adds its count.

Approach

  1. 1

    Undirected adjacency list.

  2. 2

    dist[0] = 0, ways[0] = 1, heap = [(0, 0)].

  3. 3

    For each popped node and neighbour: a shorter time overwrites dist and ways and pushes the neighbour.

  4. 4

    An equal time adds ways[node] to ways[neighbour].

  5. 5

    Return ways[n-1].

நான் எப்படித் தீர்த்தேன்

Dijkstra with path countsPythonO(E log V) time
class Solution:
    def countPaths(self, n: int, roads: List[List[int]]) -> int:
        #dijsktra's 
        MOD = 1_000_000_007
        dist = [float("inf")]*n
        ways = [0]*n
        min_heap = [(0,0)]
        dist[0]=0
        ways[0]=1
        adj = defaultdict(list)
        for u,v,w in roads:
            adj[u].append((v,w))
            adj[v].append((u,w))
        while min_heap:
            time,node = heapq.heappop(min_heap)
            for adj_node,extra_time in adj[node]:
                if time+extra_time<dist[adj_node]:
                    dist[adj_node]=time+extra_time
                    ways[adj_node]=ways[node]
                    heapq.heappush(min_heap,(time+extra_time,adj_node))
                elif dist[adj_node]==time+extra_time:
                    ways[adj_node]=(ways[adj_node]+ways[node])%MOD
        return ways[n-1]
            
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Number of Ways to Arrive at Destination — அடிக்கடி கேட்கப்படும் கேள்விகள்

Number of Ways to Arrive at Destination பின்னாலுள்ள intuition என்ன?

Run Dijkstra from 0, but alongside each node's shortest time also keep how many shortest routes reach it. A strictly faster route replaces the count; an equally fast route adds its count.

Number of Ways to Arrive at Destination (LeetCode 1976) ஐ எப்படித் தீர்ப்பது?

1. Undirected adjacency list. 2. dist[0] = 0, ways[0] = 1, heap = [(0, 0)]. 3. For each popped node and neighbour: a shorter time overwrites dist and ways and pushes the neighbour. 4. An equal time adds ways[node] to ways[neighbour]. 5. Return ways[n-1].

Number of Ways to Arrive at Destination இன் time மற்றும் space complexity என்ன?

Time: O(E log V). Space: O(V + E). (Accepted approach: Dijkstra with path counts.)

Number of Ways to Arrive at Destination இல் பொதுவான தவறுகள் என்ன?

Take the modulo when adding counts. Stale heap entries can be skipped when time > dist[node] (an optional speed-up).