Graph Theory37 sections · 1633 units
Open in Course

LeetCode 787 Cheapest Flights Within K Stops - Temporal Separation

(Why two arrays matter)

If you update distdist in place, you might use a newly-updated value in the same iteration. This allows paths longer than the iteration count. Example: Edge ABA \to B updates dist[B], then edge BCB \to C uses the new dist[B] in the same round. That allows a 22-edge path in one iteration, violating the constraint.

Using prev ensures each iteration extends paths by exactly one edge. You read from prev and write to distdist, keeping layers separate. This is temporal separation: each iteration represents one time step.