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

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