Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 787 · Shortest Path / BFS-Dijkstra

Cheapest Flights Within K Stops

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

Heap on (cost, node, stops); skip when stops > k or stops ≥ fewest stops already seen at that node.

मध्यमDynamic ProgrammingDepth-First SearchBreadth-First SearchGraph TheoryHeap (Priority Queue)

मी Cheapest Flights Within K Stops (LeetCode 787) असा सोडवला — Shortest Path / BFS-Dijkstra पॅटर्नमधील एक मध्यम प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

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

Intuition

Plain Dijkstra finds the cheapest route but ignores how many stops it uses. Put the stop count into the heap entries and prune: a node is worth expanding again only if we reach it with fewer stops than before, because a cheaper-but-longer path seen earlier may run out of stops.

Approach

  1. 1

    Build an adjacency list from the flights.

  2. 2

    Push (0, src, 0) onto a min-heap.

  3. 3

    Pop the cheapest; if it's dst, that cost is the answer.

  4. 4

    Skip entries that used too many stops or more stops than an earlier visit.

  5. 5

    Otherwise record the stops and push every neighbour with cost + price and stops + 1.

मी कसा सोडवला

Dijkstra with stop pruningPythonO(E·k log) time
class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        #we need to minimize cost
        min_heap =[(0,src,0)]#cost,node,stops
        adj = defaultdict(list)
        for u,v,w in flights:
            adj[u].append((v,w))
        stops = [float("inf")]*n
        stops[src]=0
        while min_heap:
            cost,node,steps=heapq.heappop(min_heap)
            if node==dst:
                return cost
            #if we can't reach in k stops or earlier we found a better path with less no of stops
            #we can skip
            if steps>k or steps>stops[node]:
                continue
            stops[node]=steps
            for adj_node,wt in adj[node]:
                heapq.heappush(min_heap,(cost+wt,adj_node,steps+1))
        return -1
स्टेप-थ्रू लोड होत आहे…

Cheapest Flights Within K Stops — वारंवार विचारले जाणारे प्रश्न

Cheapest Flights Within K Stops मागील intuition काय आहे?

Plain Dijkstra finds the cheapest route but ignores how many stops it uses. Put the stop count into the heap entries and prune: a node is worth expanding again only if we reach it with fewer stops than before, because a cheaper-but-longer path seen earlier may run out of stops.

Cheapest Flights Within K Stops (LeetCode 787) कसा सोडवावा?

1. Build an adjacency list from the flights. 2. Push (0, src, 0) onto a min-heap. 3. Pop the cheapest; if it's dst, that cost is the answer. 4. Skip entries that used too many stops or more stops than an earlier visit. 5. Otherwise record the stops and push every neighbour with cost + price and stops + 1.

Cheapest Flights Within K Stops ची time आणि space complexity किती आहे?

Time: O(E · k · log(E · k)). Space: O(E · k). (Accepted approach: Dijkstra with stop pruning.)

Cheapest Flights Within K Stops मध्ये सामान्य चुका कोणत्या?

Standard 'visited' marking breaks this problem — a pricier path with fewer stops may be the only valid one. k stops means k + 1 flights.